-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathminimum-swaps-to-make-strings-equal.md
44 lines (33 loc) · 1.8 KB
/
minimum-swaps-to-make-strings-equal.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<p>You are given two strings <code>s1</code> and <code>s2</code> of equal length consisting of letters <code>"x"</code> and <code>"y"</code> <strong>only</strong>. Your task is to make these two strings equal to each other. You can swap any two characters that belong to <strong>different</strong> strings, which means: swap <code>s1[i]</code> and <code>s2[j]</code>.</p>
<p>Return the minimum number of swaps required to make <code>s1</code> and <code>s2</code> equal, or return <code>-1</code> if it is impossible to do so.</p>
<p> </p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "xx", s2 = "yy"
<strong>Output:</strong> 1
<strong>Explanation:
</strong>Swap s1[0] and s2[1], s1 = "yx", s2 = "yx".</pre>
<p><strong>Example 2: </strong></p>
<pre>
<strong>Input:</strong> s1 = "xy", s2 = "yx"
<strong>Output:</strong> 2
<strong>Explanation:
</strong>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.</pre>
<p><strong>Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "xx", s2 = "xy"
<strong>Output:</strong> -1
</pre>
<p><strong>Example 4:</strong></p>
<pre>
<strong>Input:</strong> s1 = "xxyyxyxyxx", s2 = "xyyxyxxxyx"
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length <= 1000</code></li>
<li><code>s1, s2</code> only contain <code>'x'</code> or <code>'y'</code>.</li>
</ul>