leetcode-161周赛-1247-交换字符使得字符串相同
2024-08-31 20:40:37
题目描述:
自己的提交:
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)
最新文章
- js 入门级常见问题
- jsp前三章测试
- java多线程系类:基础篇:08之join
- socket协议下如何缓存图片--推荐EGOCache
- windows7文件共享
- A cycle was detected in the build path of project
- ECshop中TemplateBeginEditable 和后台编辑讲解
- 搜索(DLX重复覆盖模板):HDU 2295 Radar
- 4 weekend110的YARN的通用性意义 + yarn的job提交流程
- ISG2015
- 关于开启url的pathinfo模式
- 转 OGG add trandata 到底做了什么
- border-radius属性值参数详解
- 用Dedecms5.7的arclist标签调用文章内容
- servlet上传与下载
- Github上如何查看当前最流行的开源项目
- post请求中body数据类型
- 论文阅读笔记 Word Embeddings A Survey
- layer 弹出层
- mfc01