Problem · String

Binary String Swap Time

Learn this problem
MediumMicrosoftNEW GRADOA
See Microsoft hiring insights

Problem statement

In HackerLand, people are preparing for a parade. A red uniform must not be immediately to the left of a blue uniform.

You are given a binary string color, where:

  • '0' represents a person in a red uniform.
  • '1' represents a person in a blue uniform.

At each second, every occurrence of "01" is simultaneously changed to "10". Repeat this process until the string contains no occurrence of "01".

Return the number of seconds until the process stops.

Function

getSwapTime(color: String) → int

Examples

Example 1

color = "001011"return = 4

The string changes as follows:

t = 0: 001011
t = 1: 010101
t = 2: 101010
t = 3: 110100
t = 4: 111000

After 4 seconds, no occurrence of "01" remains.

More Microsoft problems

drafts saved locally
public int getSwapTime(String color) {
    // Write your code here
}
color"001011"
expected4
checking account