Problem · String
Binary String Swap Time
Learn this problemProblem 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) → intExamples
Example 1
color = "001011"return = 4The string changes as follows:
t = 0: 001011
t = 1: 010101
t = 2: 101010
t = 3: 110100
t = 4: 111000After 4 seconds, no occurrence of "01" remains.