【leetcode】1247. Minimum Swaps to Make Strings Equal
题目如下:
You are given two strings
s1ands2of equal length consisting of letters"x"and"y"only. Your task is to make these two strings equal to each other. You can swap any two characters that belong to different strings, which means: swaps1[i]ands2[j].Return the minimum number of swaps required to make
s1ands2equal, or return-1if it is impossible to do so.Example 1:
Input: s1 = "xx", s2 = "yy"
Output: 1
Explanation:
Swap s1[0] and s2[1], s1 = "yx", s2 = "yx".Example 2:
Input: s1 = "xy", s2 = "yx"
Output: 2
Explanation:
Swap s1[0] and s2[0], s1 = "yy", s2 = "xx".
Swap s1[0] and s2[1], s1 = "xy", s2 = "xy".
Note that you can't swap s1[0] and s1[1] to make s1 equal to "yx", cause we can only swap chars in different strings.Example 3:
Input: s1 = "xx", s2 = "xy"
Output: -1Example 4:
Input: s1 = "xxyyxyxyxx", s2 = "xyyxyxxxyx"
Output: 4Constraints:
1 <= s1.length, s2.length <= 1000s1, s2only contain'x'or'y'.
解题思路:这个题目还是有点考巧劲的。如果s1[i] != s2[i],只有这两种取值 s1[i] = x,s2[i] = y(记为xy)和s1[i] = y,s2[i] = x(记为yx)。理想情况下,进行s1[i]和s2[j]的交换,使得s1[i] = s2[i] 与 s1[j] = s2[j]同时成立,那么一定是交换次数最少的场景。而题目中例子1的交换则符合这个场景,只要分别求出(xy)和(yx)的个数,然后分别进行(xy)和(yx)的组内交换,剩余的配对再进行例子2的组间交换,即可求得最小的交换次数。
代码如下:
class Solution(object):
def minimumSwap(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: int
"""
dic = {}
dic[('x','y')] = 0
dic[('y','x')] = 0 for (i,j) in zip(s1,s2):
if i != j:
dic[(i,j)] += 1
if (dic[('x','y')] + dic[('y','x')] ) % 2 != 0:
return -1
elif dic[('x','y')] % 2 == 0 and dic[('y','x')] % 2 == 0:
return dic[('x','y')] / 2 + dic[('y','x')] / 2
return dic[('x','y')] / 2 + dic[('y','x')] / 2 + 2
【leetcode】1247. Minimum Swaps to Make Strings Equal的更多相关文章
- 【LeetCode】801. Minimum Swaps To Make Sequences Increasing 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 参考资料 日期 题目地址:https:// ...
- 【leetcode】801. Minimum Swaps To Make Sequences Increasing
题目如下: We have two integer sequences A and B of the same non-zero length. We are allowed to swap elem ...
- 【LeetCode】1151. Minimum Swaps to Group All 1's Together 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址:https://leetco ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告
今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...
- 【LeetCode】712. Minimum ASCII Delete Sum for Two Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】1071. Greatest Common Divisor of Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...
随机推荐
- 注入之Mysql-Getshell思路
- vue中 Vue.set 的使用
Vue.set(vm.items, indexOfItem, newValue) 1.vm.items :源数据:2.indexOfItem : 要修改的数据的键3.newValue : 要修改的数据 ...
- mysql——修改表名、修改字段名、修改字段数据类型、增加字段、删除字段、修改字段排列位置、修改存储引擎、删除表 (示例)
一.创建表和插入数据: ), mz ), bz ) ); ,'sww','sww01'); ,'aww','aww02'); ,'qww','qww03'), (,'eww','eww04'), (, ...
- 从零开始,SpreadJS新人学习笔记【第4周】
数据绑定.脏数据和单引号前缀 本周,让我们一起来学习SpreadJS 的数据绑定.脏数据和单引号前缀,希望我的学习笔记能够帮助你们,从零开始学习 SpreadJS,并逐步精通. 在此前的学习笔记中,相 ...
- Oracle RAC安装文档
http://www.itpub.net/thread-1895813-2-1.html chkconfig iptables offservice iptables stop 1.修改主机名rac1 ...
- HTML页面左上角图标
显示网页左上角标志图标 <link rel="shortcut icon"type="image/x-icon"href="images/fav ...
- Codeforces 1196F. K-th Path
传送门 发现到 $K$ 不大,考虑有什么和 $K$ 有关的结论 发现答案似乎只会经过前 $K$ 小的边,如果边权第 $K$ 小的边有多条那么可以任意取 证明挺显然的吧,首先如果走了边权排名大于 $K$ ...
- Codeforces 1221E. Game With String
传送门 首先每一段连续的 $...$ 都是互不影响的,所以可以一段段考虑 考虑最简单的情况,此时每一段都大于等于 $a$ 并且小于 $2b$ ,那么每一段都只能放一次,胜负直接根据段数即可得到答案 考 ...
- Spring Cloud Alibaba nacos 配置中心使用
背景 上一文我们讲到了如何去搭建注册中心,这一次我们讲述如何使用nacos作为注册中心 spring-cloud-alibaba-basis 创建基础依赖 首先我们创建一个spring-cloud-a ...
- centos配置mutt和msmtp发送邮件
一.安装mutt yum install mutt -y 二.配置mutt vim /etc/Muttrc 在里面找到下面几行,并将内容修改为你自己的内容(下面几行分布在不同位置,请耐心查找,记得去掉 ...