FastPrepMinimum Swaps to Make Strings Equal

Minimum Swaps to Make Strings Equal

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

You are given two equal-length strings first and second containing only 'x' and 'y'. In one operation, choose one position from each string and swap those two characters.

Return the minimum operations needed to make the strings equal, or -1 if it is impossible.

Function

minimumSwap(first: String, second: String) → int

Examples

Example 1

first = "xx"second = "yy"return = 1

Swap one x with one y across the strings.

Example 2

first = "xy"second = "yx"return = 2

The two opposite mismatch types require two swaps.

Constraints

  • 1 <= first.length == second.length <= 10^5.
  • Both strings contain only 'x' and 'y'.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int minimumSwap(String first, String second) {
    // Write your code here.
}
first"xx"
second"yy"
expected1
Checking account…