LeetCode Problem

How to Solve Minimum Swaps to Make Strings Equal

To solve this problem, swap characters from two strings s1 and s2 at mismatched positions. A greedy approach with invariant validation helps minimize the swap count. If it's impossible, return -1.

GhostInterview Help

Need help with Minimum Swaps to Make Strings Equal without spending extra time grinding it?

GhostInterview can read Minimum Swaps to Make Strings Equal from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #1247Greedy choice plus invariant validationReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Greedy choice plus invariant validation
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve this problem, swap characters from two strings s1 and s2 at mismatched positions. A greedy approach with invariant validation helps minimize the swap count. If it's impossible, return -1.

Problem Statement

You are given two strings, s1 and s2, of equal length consisting only of the characters 'x' and 'y'. Your task is to make these two strings equal by swapping characters between them. You are allowed to swap s1[i] and s2[j], but you cannot swap characters within the same string.

Return the minimum number of swaps required to make s1 and s2 equal, or return -1 if it is impossible. If both strings contain an unequal number of 'x' and 'y', a solution is not possible.

Examples

Example 1

Input: s1 = "xx", s2 = "yy"

Output: 1

Swap s1[0] and s2[1], s1 = "yx", s2 = "yx".

Example 2

Input: s1 = "xy", s2 = "yx"

Output: 2

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

Example details omitted.

Constraints

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

Solution Approach

Greedy Approach with Invariant Validation

First, identify the positions where the characters in s1 and s2 are unmatched. Only consider these positions for swapping. The basic idea is to count mismatched pairs of 'x' and 'y' between the two strings and then determine how to minimize the swap count based on these mismatches.

Mathematical Analysis of Mismatches

Check if the number of 'x' and 'y' characters in both strings is the same. If not, return -1 since it's impossible to make the strings equal. Once validated, focus on the number of unmatched pairs and compute the minimum swaps required by applying a greedy method.

Edge Case Handling

Edge cases, such as strings that are already equal, must be handled by ignoring positions where characters are already matched. Also, ensure that cases with impossible solutions due to unequal counts of 'x' or 'y' in both strings are identified early.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity is O(n), where n is the length of the strings, as we only need to scan through the strings once to count mismatches. The space complexity is O(1), since we are using only a constant amount of extra space for counting mismatches.

What Interviewers Usually Probe

  • Candidates should demonstrate an understanding of greedy algorithms and invariant validation.
  • Look for an ability to handle edge cases like unequal counts of 'x' or 'y' in the two strings.
  • Candidates who struggle with the swap count calculation or the impossibility check may need further guidance.

Common Pitfalls or Variants

Common pitfalls

  • Failing to check for the basic condition of equal 'x' and 'y' counts in both strings, which leads to unnecessary work or incorrect answers.
  • Overcomplicating the swap logic or not considering the invariant validation that matches mismatched pairs correctly.
  • Not properly handling edge cases where strings are already equal or impossible to solve.

Follow-up variants

  • Strings of larger lengths, requiring optimization of the greedy approach.
  • Introducing characters beyond 'x' and 'y' and extending the logic to handle more characters.
  • Modifying the problem to allow swapping within the same string.

How GhostInterview Helps

  • GhostInterview assists by offering detailed explanations of the greedy approach, helping to identify key invariant patterns in mismatched positions.
  • It guides you through step-by-step reasoning for handling edge cases and providing the correct logic for swap counting.
  • By understanding common pitfalls and interview signals, GhostInterview helps you avoid mistakes and practice solving the problem efficiently.

Topic Pages

FAQ

What is the main pattern in the Minimum Swaps to Make Strings Equal problem?

The main pattern is greedy choice plus invariant validation, focusing on minimizing swaps by first ensuring mismatched positions are handled correctly.

How can I optimize this problem for larger input sizes?

The solution should already be optimized to O(n) time complexity, but focus on ensuring that the solution scales well by handling the swap logic efficiently.

What if the two strings have unequal counts of 'x' and 'y'?

If the counts of 'x' and 'y' in both strings are not equal, the problem is unsolvable, and you should return -1.

How do I handle edge cases where the strings are already equal?

In such cases, you simply return 0, as no swaps are needed to make the strings equal.

Can the logic for this problem be extended to other characters beyond 'x' and 'y'?

Yes, the greedy approach can be generalized to work with other characters, but you need to adjust the mismatch pair counting logic accordingly.

GhostInterview Solver

Need direct help with Minimum Swaps to Make Strings Equal instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Minimum Swaps to Make Strings Equal from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.