Skip to content

Practice Minimum Swaps to Make Strings Equal

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1247
LeetCode Problem

Minimum Swaps to Make Strings Equal

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.

Source: Minimum Swaps to Make Strings Equal on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s1 = "xx", s2 = "yy"
Output
1

# Example 2

Input
s1 = "xy", s2 = "yx"
Output
2

# 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'.

Related LeetCode Problems

Practice more problems similar to Minimum Swaps to Make Strings Equal.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.