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.
More Microsoft problems
- Minimum Effort Task ScheduleOA · Seen Jul 2026
- Maximum Pipeline ThroughputOA · Seen Jul 2026
- Maximum Strong Team SubarrayOA · Seen Jul 2026
- Minimum Cost K-Capable ModelsOA · Seen Jul 2026
- Alphabetically Smallest PalindromeOA · Seen Jul 2026
- Maximum Reward PointsOA · Seen Jul 2026
- Maximum Strength of Every NeuronOA · Seen Jul 2026
- Neural Network Subnetwork StrengthOA · Seen Jul 2026