Formatted question description: https://leetcode.ca/all/1247.html

1247. Minimum Swaps to Make Strings Equal

Level

Medium

Description

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 can’t swap s1[0] and s1[1] to make s1 equal to “yx”, cause we can only swap chars in different strings.

Example 3:

Input: s1 = “xx”, s2 = “xy”

Output: -1

Example 4:

Input: s1 = “xxyyxyxyxx”, s2 = “xyyxyxxxyx”

Output: 4

Constraints:

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

Solution

Loop over s1 and s2 and find the indices where the characters in s1 and s2 are different. Use two lists difX and difY to store the indices where the character in s1 is 'x' and the character in s2 is 'y', and the indices where the character in s1 is 'y' and the character in s2 is 'x', respectively.

Calculate sizeX = difX.size() and sizeY = difY.size(). If sizeX + sizeY is odd, then it is impossible to swap the characters to make the two strings equal, so return -1. If sizeX + sizeY is even, then sizeX and sizeY are both even or both odd. If both even, then return (sizeX + sizeY) / 2. If both odd, then return (sizeX + sizeY) / 2 + 1.

The explanation on the return value is as follows.

  1. If sizeX and sizeY are both even, then for the sizeX indices where the character in s1 is 'x' and the character in s2 is 'y', let indices1 and indices2 are two disjoint subsets of difX such that indices1 and indices2 have equal number of elements and the union of indices1 and indices2 equals difX. Replace each character in s1 at indices from indices1 with each character in s2 at indices from indices2. Do the same for the other sizeY indices as well. Thus the total number of swaps is (sizeX + sizeY) / 2.

  2. If sizeX and sizeY are both odd, then perform the steps above for sizeX - 1 indices from difX and sizeY - 1 indices from difY, and the total numbers of swaps is (sizeX + sizeY) / 2 - 1 so far. After that, there are two indices left, where the subsequences are "xy" and "yx" in s1 and s2, respectively. To make the subsequences equal, two swaps are needed. Thus the total number of swaps is (sizeX + sizeY) / 2 + 1.

class Solution {
    public int minimumSwap(String s1, String s2) {
        List<Integer> difX = new ArrayList<Integer>();
        List<Integer> difY = new ArrayList<Integer>();
        int length = s1.length();
        for (int i = 0; i < length; i++) {
            char c1 = s1.charAt(i), c2 = s2.charAt(i);
            if (c1 != c2) {
                if (c1 == 'x')
                    difX.add(i);
                else
                    difY.add(i);
            }
        }
        int sizeX = difX.size(), sizeY = difY.size();
        if ((sizeX + sizeY) % 2 != 0)
            return -1;
        else if (sizeX % 2 == 0)
            return (sizeX + sizeY) / 2;
        else
            return (sizeX + sizeY) / 2 + 1;
    }
}

All Problems

All Solutions