Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Minimum Swaps to Make Strings Equal

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1247" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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", 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. 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'.

Explanation

Here's the breakdown of the solution:

  • Count Mismatches: The core idea is to count the number of 'x' in s1 that correspond to a 'y' in s2 and vice versa. These mismatches need to be resolved by swaps.

  • Analyze Mismatch Parity: If the number of 'x'/'y' mismatches is odd, it's impossible to make the strings equal because each swap resolves two mismatches (either 'x' in s1 with 'y' in s2 or the reverse).

  • Calculate Swaps: If possible, pairs of 'xx' mismatches can be resolved with one swap, and pairs of 'yy' mismatches can be resolved with one swap. If there is one remaining 'x'/'y' mismatch in both s1 and s2, then two swaps are required.

  • Time & Space Complexity: O(n) time and O(1) space, where n is the length of the strings.

Code

    def minSwaps(s1: str, s2: str) -> int:
    """
    Calculates the minimum number of swaps required to make two strings equal.

    Args:
        s1: The first string.
        s2: The second string.

    Returns:
        The minimum number of swaps, or -1 if it is impossible.
    """

    n = len(s1)
    x_y = 0  # Count of s1[i] == 'x' and s2[i] == 'y'
    y_x = 0  # Count of s1[i] == 'y' and s2[i] == 'x'

    for i in range(n):
        if s1[i] == 'x' and s2[i] == 'y':
            x_y += 1
        elif s1[i] == 'y' and s2[i] == 'x':
            y_x += 1

    if (x_y + y_x) % 2 != 0:
        return -1

    swaps = x_y // 2 + y_x // 2

    if x_y % 2 != 0:  # Equivalent to y_x % 2 != 0, since (x_y + y_x) is even
        swaps += 2

    return swaps

More from this blog

C

Chatmagic blog

2894 posts