1247:交换字符使得字符串相同

This commit is contained in:
huangge1199 2021-09-08 15:53:15 +08:00
parent 51eb90bb37
commit 1b0f9df876
2 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1,83 @@
//有两个长度相同的字符串 s1 s2且它们其中 只含有 字符 "x" "y"你需要通过交换字符的方式使这两个字符串相同
//
// 每次交换字符的时候你都可以在两个字符串中各选一个字符进行交换
//
// 交换只能发生在两个不同的字符串之间绝对不能发生在同一个字符串内部也就是说我们可以交换 s1[i] s2[j]但不能交换 s1[i] s1[
//j]
//
// 最后请你返回使 s1 s2 相同的最小交换次数如果没有方法能够使得这两个字符串相同则返回 -1
//
//
//
// 示例 1
//
// 输入s1 = "xx", s2 = "yy"
//输出1
//解释
//交换 s1[0] s2[1]得到 s1 = "yx"s2 = "yx"
//
// 示例 2
//
// 输入s1 = "xy", s2 = "yx"
//输出2
//解释
//交换 s1[0] s2[0]得到 s1 = "yy"s2 = "xx"
//交换 s1[0] s2[1]得到 s1 = "xy"s2 = "xy"
//注意你不能交换 s1[0] s1[1] 使得 s1 变成 "yx"因为我们只能交换属于两个不同字符串的字符
//
// 示例 3
//
// 输入s1 = "xx", s2 = "xy"
//输出-1
//
//
// 示例 4
//
// 输入s1 = "xxyyxyxyxx", s2 = "xyyxyxxxyx"
//输出4
//
//
//
//
// 提示
//
//
// 1 <= s1.length, s2.length <= 1000
// s1, s2 只包含 'x' 'y'
//
// Related Topics 贪心 数学 字符串 👍 45 👎 0
package leetcode.editor.cn;
//1247:交换字符使得字符串相同
class MinimumSwapsToMakeStringsEqual {
public static void main(String[] args) {
//测试代码
Solution solution = new MinimumSwapsToMakeStringsEqual().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int minimumSwap(String s1, String s2) {
int x = 0;
int y = 0;
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) != s2.charAt(i)) {
if (s1.charAt(i) == 'x') {
x++;
} else {
y++;
}
}
}
if ((x + y) % 2 == 1) {
return -1;
}
int min = Math.min(x, y);
return 2 * (min % 2) + x / 2 + y / 2;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,47 @@
<p>有两个长度相同的字符串&nbsp;<code>s1</code>&nbsp;<code>s2</code>,且它们其中&nbsp;<strong>只含有</strong>&nbsp;字符&nbsp;<code>&quot;x&quot;</code>&nbsp;<code>&quot;y&quot;</code>,你需要通过「交换字符」的方式使这两个字符串相同。</p>
<p>每次「交换字符」的时候,你都可以在两个字符串中各选一个字符进行交换。</p>
<p>交换只能发生在两个不同的字符串之间,绝对不能发生在同一个字符串内部。也就是说,我们可以交换&nbsp;<code>s1[i]</code>&nbsp;<code>s2[j]</code>,但不能交换&nbsp;<code>s1[i]</code>&nbsp;<code>s1[j]</code></p>
<p>最后,请你返回使 <code>s1</code><code>s2</code> 相同的最小交换次数,如果没有方法能够使得这两个字符串相同,则返回&nbsp;<code>-1</code></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>s1 = &quot;xx&quot;, s2 = &quot;yy&quot;
<strong>输出:</strong>1
<strong>解释:
</strong>交换 s1[0] 和 s2[1],得到 s1 = &quot;yx&quot;s2 = &quot;yx&quot;</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>s1 = &quot;xy&quot;, s2 = &quot;yx&quot;
<strong>输出:</strong>2
<strong>解释:
</strong>交换 s1[0] 和 s2[0],得到 s1 = &quot;yy&quot;s2 = &quot;xx&quot;
交换 s1[0] 和 s2[1],得到 s1 = &quot;xy&quot;s2 = &quot;xy&quot;
注意,你不能交换 s1[0] 和 s1[1] 使得 s1 变成 &quot;yx&quot;,因为我们只能交换属于两个不同字符串的字符。</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>s1 = &quot;xx&quot;, s2 = &quot;xy&quot;
<strong>输出:</strong>-1
</pre>
<p><strong>示例 4</strong></p>
<pre><strong>输入:</strong>s1 = &quot;xxyyxyxyxx&quot;, s2 = &quot;xyyxyxxxyx&quot;
<strong>输出:</strong>4
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s1.length, s2.length &lt;= 1000</code></li>
<li><code>s1, s2</code>&nbsp;只包含&nbsp;<code>&#39;x&#39;</code>&nbsp;&nbsp;<code>&#39;y&#39;</code></li>
</ul>
<div><div>Related Topics</div><div><li>贪心</li><li>数学</li><li>字符串</li></div></div><br><div><li>👍 45</li><li>👎 0</li></div>