Minimum Swaps to Make Strings Equal

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 swapping 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
Inputs1 = "xx", s2 = "yy"
Output1
Swapping s1[0] and s2[1] makes both strings equal to "yx".
Example 2
Inputs1 = "xy", s2 = "yx"
Output2
Two swaps can make both strings equal to "xy", and swapping within the same string is not allowed.

Constraints

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

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate