1247:交换字符使得字符串相同
This commit is contained in:
parent
51eb90bb37
commit
1b0f9df876
@ -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)
|
||||
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
<p>有两个长度相同的字符串 <code>s1</code> 和 <code>s2</code>,且它们其中 <strong>只含有</strong> 字符 <code>"x"</code> 和 <code>"y"</code>,你需要通过「交换字符」的方式使这两个字符串相同。</p>
|
||||
|
||||
<p>每次「交换字符」的时候,你都可以在两个字符串中各选一个字符进行交换。</p>
|
||||
|
||||
<p>交换只能发生在两个不同的字符串之间,绝对不能发生在同一个字符串内部。也就是说,我们可以交换 <code>s1[i]</code> 和 <code>s2[j]</code>,但不能交换 <code>s1[i]</code> 和 <code>s1[j]</code>。</p>
|
||||
|
||||
<p>最后,请你返回使 <code>s1</code> 和 <code>s2</code> 相同的最小交换次数,如果没有方法能够使得这两个字符串相同,则返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s1 = "xx", s2 = "yy"
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:
|
||||
</strong>交换 s1[0] 和 s2[1],得到 s1 = "yx",s2 = "yx"。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s1 = "xy", s2 = "yx"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:
|
||||
</strong>交换 s1[0] 和 s2[0],得到 s1 = "yy",s2 = "xx" 。
|
||||
交换 s1[0] 和 s2[1],得到 s1 = "xy",s2 = "xy" 。
|
||||
注意,你不能交换 s1[0] 和 s1[1] 使得 s1 变成 "yx",因为我们只能交换属于两个不同字符串的字符。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s1 = "xx", s2 = "xy"
|
||||
<strong>输出:</strong>-1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s1 = "xxyyxyxyxx", s2 = "xyyxyxxxyx"
|
||||
<strong>输出:</strong>4
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s1.length, s2.length <= 1000</code></li>
|
||||
<li><code>s1, s2</code> 只包含 <code>'x'</code> 或 <code>'y'</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>
|
Loading…
Reference in New Issue
Block a user