Minimum Swaps to Make Strings Equal - Complete Solution Guide
Minimum Swaps to Make Strings Equal is LeetCode problem 1247, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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"
Detailed Explanation
The problem requires finding the minimum number of swaps between characters in two strings, `s1` and `s2`, to make them equal. The strings consist only of 'x' and 'y' characters, and swaps can only occur between `s1` and `s2` (not within a single string). If it's impossible to make the strings equal, return -1. Strings have equal length.
Solution Approach
The solution counts the number of 'xy' mismatches (`xy_mismatches`) and 'yx' mismatches (`yx_mismatches`). If the total number of mismatches is odd, it's impossible to equalize the strings, and -1 is returned. Otherwise, it calculates the number of swaps needed. Pairs of 'xy' and 'yx' mismatches are handled directly (e.g., two 'xy' mismatches require one swap). Finally, if there is a single remaining 'xy' and a single remaining 'yx' (resulting in xy_mismatches % 2 == 1), it necessitates two additional swaps.
Step-by-Step Algorithm
- Step 1: Initialize `xy_mismatches` and `yx_mismatches` to 0.
- Step 2: Iterate through the strings `s1` and `s2` simultaneously using `zip` (or index-based iteration in other languages).
- Step 3: In each iteration, compare the characters at the current index. If they are different, increment `xy_mismatches` if `s1[i]` is 'x', or increment `yx_mismatches` if `s1[i]` is 'y'.
- Step 4: After the loop, check if the sum of `xy_mismatches` and `yx_mismatches` is odd. If it is, return -1.
- Step 5: Calculate the minimum number of swaps as `(xy_mismatches // 2) + (yx_mismatches // 2)`. This accounts for pairs of 'xy' and 'yx' mismatches requiring one swap each.
- Step 6: If `xy_mismatches % 2 == 1`, it means there's one remaining 'xy' mismatch and one remaining 'yx' mismatch, which requires two additional swaps. Add 2 to the swap count.
- Step 7: Return the calculated swap count.
Key Insights
- Insight 1: The problem is fundamentally about counting mismatches. Specifically, we need to count the number of 'x' in `s1` that correspond to 'y' in `s2` and vice-versa. These counts determine the swap operations needed.
- Insight 2: The sum of mismatched 'x' and 'y' must be even for the strings to be potentially equalizable. If the sum is odd, it's impossible to equalize the strings because each swap corrects two mismatches.
- Insight 3: Two 'xy' mismatches can be corrected with one swap. Two 'yx' mismatches can also be corrected with one swap. If we have a single 'xy' and a single 'yx' mismatch, it requires two swaps.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Math, String, Greedy.
Companies
Asked at: J.P. Morgan.