Problem · String
Time Needed to Rearrange a Binary String
Learn this problemProblem statement
You are given a binary string s. During each second, replace every occurrence of 01 with 10 simultaneously.
Repeat the process until s contains no occurrence of 01. Return the number of seconds required.
Function
secondsToRearrange(s: String) → intExamples
Example 1
s = "0110101"return = 4The successive strings are 1011010, 1101100, 1110100, and 1111000. After 4 seconds, no 01 remains.
Example 2
s = "11100"return = 0The string already contains no 01, so the process requires 0 seconds.
Constraints
1 <= s.length <= 1000- Every character of
sis either0or1.
More Salesforce problems
- Diameter of an Acyclic Undirected GraphONSITE INTERVIEW · Seen Jul 2026
- Optimal Account BalancingPHONE SCREEN · Seen Jul 2026
- Longest Increasing SubsequencePHONE SCREEN · Seen Jul 2026
- Maximal SquarePHONE SCREEN · Seen Jul 2026
- Maximum Barbell WeightOA · Seen Jul 2026
- Minimum No-Repeat Segments After One Character RemovalOA · Seen Jul 2026
- Minimum Operations to ZeroOA · Seen Jul 2026
- Minimize Total Input Cost (for LTMS)OA · Seen Jun 2026