题目如下:

You are given two strings s1 and s2 of 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: swap s1[i] and s2[j].

Return the minimum number of swaps required to make s1 and s2 equal, or return -1 if it is impossible to do so.

Example 1:

  1. Input: s1 = "xx", s2 = "yy"
  2. Output: 1
  3. Explanation:
  4. Swap s1[0] and s2[1], s1 = "yx", s2 = "yx".

Example 2:

  1. Input: s1 = "xy", s2 = "yx"
  2. Output: 2
  3. Explanation:
  4. Swap s1[0] and s2[0], s1 = "yy", s2 = "xx".
  5. Swap s1[0] and s2[1], s1 = "xy", s2 = "xy".
  6. 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:

  1. Input: s1 = "xx", s2 = "xy"
  2. Output: -1

Example 4:

  1. Input: s1 = "xxyyxyxyxx", s2 = "xyyxyxxxyx"
  2. Output: 4

Constraints:

  • 1 <= s1.length, s2.length <= 1000
  • s1, s2 only 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的组间交换,即可求得最小的交换次数。

代码如下:

  1. class Solution(object):
  2. def minimumSwap(self, s1, s2):
  3. """
  4. :type s1: str
  5. :type s2: str
  6. :rtype: int
  7. """
  8. dic = {}
  9. dic[('x','y')] = 0
  10. dic[('y','x')] = 0
  11.  
  12. for (i,j) in zip(s1,s2):
  13. if i != j:
  14. dic[(i,j)] += 1
  15. if (dic[('x','y')] + dic[('y','x')] ) % 2 != 0:
  16. return -1
  17. elif dic[('x','y')] % 2 == 0 and dic[('y','x')] % 2 == 0:
  18. return dic[('x','y')] / 2 + dic[('y','x')] / 2
  19. return dic[('x','y')] / 2 + dic[('y','x')] / 2 + 2

【leetcode】1247. Minimum Swaps to Make Strings Equal的更多相关文章

  1. 【LeetCode】801. Minimum Swaps To Make Sequences Increasing 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 参考资料 日期 题目地址:https:// ...

  2. 【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 ...

  3. 【LeetCode】1151. Minimum Swaps to Group All 1's Together 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址:https://leetco ...

  4. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  5. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  6. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

  7. 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告

    今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...

  8. 【LeetCode】712. Minimum ASCII Delete Sum for Two Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】1071. Greatest Common Divisor of Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...

随机推荐

  1. 【VS开发】【图像处理】直方图均衡与平台直方图

    目录(?)[-] 直方图均衡化Histogram Equalization 直方图均衡化的主要过程 一个简单的例子 关键的代码实现 平台直方图及均衡化 平台直方图的概念 平台阈值的确定 关键代码实现 ...

  2. 【Linux】linux设备驱动归纳总结

    前言: (总结已经基本写完,这段时间我会从新排版和修正.错误总会有的,望能指正!) 前段时间学习了嵌入式驱动,趁着没开始找工作,这段时间我会每天抽出时间来复习. 我的总结是根据学习时的笔记(李杨老师授 ...

  3. LINQ查询表达式详解(1)——基本语法、使用扩展方法和Lambda表达式简化LINQ查询

    简介 使用线程的主要原因:应用程序中一些操作需要消耗一定的时间,比如对文件.数据库.网络的访问等等,而我们不希望用户一直等待到操作结束,而是在此同时可以进行一些其他的操作.  这就可以使用线程来实现. ...

  4. 【数据库】Redis/MongoDB/MySQL/Oracle随笔索引

    数据库体系 [思维导图]数据库体系 密码: a8ni Redis JPA

  5. [转帖]运行时库(runtime library)

    运行时库(runtime library) https://blog.csdn.net/xitie8523/article/details/82712105 没学过这些东西 或者当时上课没听 又或者 ...

  6. 从入门到自闭之Python while如何使用

    while 循环 ​ while 条件: ​ 循环体 终止循环的两种办法: 改变条件 break break和continue的用法: break 用法:打破当前循环,(终止当前循环),所处位置在循环 ...

  7. 禁用Win10自带截图工具快捷键(Shift+Win+S)

    由于在微信之前,多年使用QQ的缘故,已经习惯了使用Ctrl+Alt+A进行截图,虽然QQ后来还专门提供了TIM(Office-QQ),但仍然渐渐的以微信为主,TIM甚至已经很少登录,之前登录也仅仅是为 ...

  8. js中的奇闻异事

  9. C#中static修饰符的作用

    static在C#中表示的是静态的,比如一个静态的字段是归类型所有,而非归对象所有,也就是说,在调用这个字段时,只能用类型去调,而不能用对象. 实例字段时随着对象创建而创建,对象销毁而销毁,而静态字段 ...

  10. SQL数据库字段数据类型详细说明

    这里先总结数据类型.MySQL中的数据类型大的方面来分,可以分为:日期和时间.数值,以及字符串.下面就分开来进行总结. 日期和时间数据类型 MySQL数据类型 含义 date 3字节,日期,格式:20 ...