题目描述:

自己的提交:

class Solution:
def minimumSwap(self, s1: str, s2: str) -> int:
count = {"xy":0,"yx":0}
for i in range(len(s1)):
if s1[i] == s2[i]:
continue
elif s1[i] == "x" and s2[i] == "y":
count["xy"] += 1
elif s1[i] == "y" and s2[i] == "x":
count["yx"] += 1
m = count["xy"] % 2
n = count["yx"] % 2
if [m,n] in [[1,0],[0,1]]:
return -1
else:
return count["xy"]//2 + count["yx"]//2 + m + n

优化:

class Solution:
def minimumSwap(self, s1: str, s2: str) -> int:
if len(s1) != len(s2): return -1
xy = yx = 0
for c1, c2 in zip(s1, s2):
if c1 == c2: continue
if c1 == 'x': xy += 1
else: yx += 1
if xy&1 != yx&1: return -1
return ((xy+1)>>1) + ((yx+1)>>1)

最新文章

  1. js 入门级常见问题
  2. jsp前三章测试
  3. java多线程系类:基础篇:08之join
  4. socket协议下如何缓存图片--推荐EGOCache
  5. windows7文件共享
  6. A cycle was detected in the build path of project
  7. ECshop中TemplateBeginEditable 和后台编辑讲解
  8. 搜索(DLX重复覆盖模板):HDU 2295 Radar
  9. 4 weekend110的YARN的通用性意义 + yarn的job提交流程
  10. ISG2015
  11. 关于开启url的pathinfo模式
  12. 转 OGG add trandata 到底做了什么
  13. border-radius属性值参数详解
  14. 用Dedecms5.7的arclist标签调用文章内容
  15. servlet上传与下载
  16. Github上如何查看当前最流行的开源项目
  17. post请求中body数据类型
  18. 论文阅读笔记 Word Embeddings A Survey
  19. layer 弹出层
  20. mfc01

热门文章

  1. 离线安装gcc_rpm(centos下安装gcc的方法之一)
  2. PHP实现笛卡尔积算法
  3. C# 使用猫拨打电话
  4. Android Studio 打包生成正式apk(XXX-release.apk)的两种方式
  5. 【InnoDB】缓冲池
  6. Oracle with as语法
  7. 老板让我十分钟上手nx-admin
  8. spss乱码问题解决
  9. 14. Django MTV及Django模型
  10. git submodule 删除及修改