1247. Minimum Swaps to Make Strings Equal LeetCode Solution

In this guide, you will get 1247. Minimum Swaps to Make Strings Equal LeetCode Solution with the best time and space complexity. The solution to Minimum Swaps to Make Strings Equal problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Swaps to Make Strings Equal solution in C++
  4. Minimum Swaps to Make Strings Equal solution in Java
  5. Minimum Swaps to Make Strings Equal solution in Python
  6. Additional Resources
1247. Minimum Swaps to Make Strings Equal LeetCode Solution image

Problem Statement of Minimum Swaps to Make Strings Equal

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:

Input: s1 = “xx”, s2 = “yy”
Output: 1
Explanation: Swap s1[0] and s2[1], s1 = “yx”, s2 = “yx”.

Example 2:

Input: s1 = “xy”, s2 = “yx”
Output: 2
Explanation: Swap s1[0] and s2[0], s1 = “yy”, s2 = “xx”.
Swap s1[0] and s2[1], s1 = “xy”, s2 = “xy”.
Note that you cannot swap s1[0] and s1[1] to make s1 equal to “yx”, cause we can only swap chars in different strings.

See also  148. Sort List LeetCode Solution

Example 3:

Input: s1 = “xx”, s2 = “xy”
Output: -1

Constraints:

1 <= s1.length, s2.length <= 1000
s1.length == s2.length
s1, s2 only contain 'x' or 'y'.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1247. Minimum Swaps to Make Strings Equal LeetCode Solution in C++

class Solution {
 public:
  int minimumSwap(string s1, string s2) {
    // ("xx", "yy") = (2 "xy"s) -> 1 swap
    // ("yy", "xx") = (2 "yx"s) -> 1 swap
    // ("xy", "yx") = (1 "xy" and 1 "yx") -> 2 swaps
    int xy = 0;  // the number of indices i's s.t. s1[i] = 'x' and s2[i] 'y'
    int yx = 0;  // the number of indices i's s.t. s1[i] = 'y' and s2[i] 'x'

    for (int i = 0; i < s1.length(); ++i) {
      if (s1[i] == s2[i])
        continue;
      if (s1[i] == 'x')
        ++xy;
      else
        ++yx;
    }

    if ((xy + yx) % 2 == 1)
      return -1;
    return xy / 2 + yx / 2 + (xy % 2 == 0 ? 0 : 2);
  }
};
/* code provided by PROGIEZ */

1247. Minimum Swaps to Make Strings Equal LeetCode Solution in Java

class Solution {
  public int minimumSwap(String s1, String s2) {
    // ("xx", "yy") = (2 "xy"s) -> 1 swap
    // ("yy", "xx") = (2 "yx"s) -> 1 swap
    // ("xy", "yx") = (1 "xy" and 1 "yx") -> 2 swaps
    int xy = 0; // the number of indices i's s.t. s1[i] = 'x' and s2[i] 'y'
    int yx = 0; // the number of indices i's s.t. s1[i] = 'y' and s2[i] 'x'

    for (int i = 0; i < s1.length(); ++i) {
      if (s1.charAt(i) == s2.charAt(i))
        continue;
      if (s1.charAt(i) == 'x')
        ++xy;
      else
        ++yx;
    }

    if ((xy + yx) % 2 == 1)
      return -1;
    return xy / 2 + yx / 2 + (xy % 2 == 0 ? 0 : 2);
  }
}
// code provided by PROGIEZ

1247. Minimum Swaps to Make Strings Equal LeetCode Solution in Python

class Solution:
  def minimumSwap(self, s1: str, s2: str) -> int:
    # ('xx', 'yy') = (2 'xy's) . 1 swap
    # ('yy', 'xx') = (2 'yx's) . 1 swap
    # ('xy', 'yx') = (1 'xy' and 1 'yx') . 2 swaps
    xy = 0  # the number of indices i's s.t. s1[i] = 'x' and s2[i] 'y'
    yx = 0  # the number of indices i's s.t. s1[i] = 'y' and s2[i] 'x'

    for a, b in zip(s1, s2):
      if a == b:
        continue
      if a == 'x':
        xy += 1
      else:
        yx += 1

    if (xy + yx) % 2 == 1:
      return -1
    return xy // 2 + yx // 2 + (0 if xy % 2 == 0 else 2)
# code by PROGIEZ

Additional Resources

See also  1035. Uncrossed Lines LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.