Problem · String

Time Needed to Rearrange a Binary String

Learn this problem
MediumSalesforce logoSalesforceFULLTIMEOA
See Salesforce hiring insights

Problem 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) → int

Examples

Example 1

s = "0110101"return = 4

The successive strings are 1011010, 1101100, 1110100, and 1111000. After 4 seconds, no 01 remains.

Example 2

s = "11100"return = 0

The string already contains no 01, so the process requires 0 seconds.

Constraints

  • 1 <= s.length <= 1000
  • Every character of s is either 0 or 1.

More Salesforce problems

drafts saved locally
public int secondsToRearrange(String s) {
    // Write your code here.
}
s"0110101"
expected4
checking account