java.lang.Object
g1201_1300.s1247_minimum_swaps_to_make_strings_equal.Solution

public class Solution extends Object
1247 - Minimum Swaps to Make Strings Equal.<p>Medium</p> <p>You are given two strings <code>s1</code> and <code>s2</code> of equal length consisting of letters <code>&quot;x&quot;</code> and <code>&quot;y&quot;</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><strong>Example 1:</strong></p> <p><strong>Input:</strong> s1 = &ldquo;xx&rdquo;, s2 = &ldquo;yy&rdquo;</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> Swap s1[0] and s2[1], s1 = &ldquo;yx&rdquo;, s2 = &ldquo;yx&rdquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s1 = &ldquo;xy&rdquo;, s2 = &ldquo;yx&rdquo;</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <p>Swap s1[0] and s2[0], s1 = &ldquo;yy&rdquo;, s2 = &ldquo;xx&rdquo;.</p> <p>Swap s1[0] and s2[1], s1 = &ldquo;xy&rdquo;, s2 = &ldquo;xy&rdquo;.</p> <p>Note that you cannot swap s1[0] and s1[1] to make s1 equal to &ldquo;yx&rdquo;, cause we can only swap chars in different strings.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s1 = &ldquo;xx&rdquo;, s2 = &ldquo;xy&rdquo;</p> <p><strong>Output:</strong> -1</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>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minimumSwap

      public int minimumSwap(String s1, String s2)