You are given a binary string s
. In one second, all occurrences of "01"
are simultaneously replaced with "10"
. This process repeats until no occurrences of "01"
exist.
Return the number of seconds needed to complete this process.
Example 1:
Input: s = "0110101" Output: 4 Explanation: After one second, s becomes "1011010". After another second, s becomes "1101100". After the third second, s becomes "1110100". After the fourth second, s becomes "1111000". No occurrence of "01" exists any longer, and the process needed 4 seconds to complete, so we return 4.
Example 2:
Input: s = "11100" Output: 0 Explanation: No occurrence of "01" exists in s, and the processes needed 0 seconds to complete, so we return 0.
Constraints:
1 <= s.length <= 1000
s[i]
is either '0'
or '1'
.Follow up:
Can you solve this problem in O(n) time complexity?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for rearranging a binary string involves repeatedly swapping adjacent characters. We keep doing this until the string is sorted according to the desired pattern, calculating the time taken for each swap.
Here's how the algorithm would work step-by-step:
def time_needed_to_rearrange(
binary_string):
total_time = 0
while True:
swapped = False
string_list = list(binary_string)
for index in range(
len(string_list) - 1):
# Check for out-of-order pairs ('1' before '0')
if (
string_list[index] == '1'
and string_list[index + 1] == '0'):
# Swap the adjacent characters
string_list[index], \
string_list[index + 1] = \
string_list[index + 1], \
string_list[index]
binary_string = "".join(
string_list)
total_time += 1
swapped = True
break #Only perform one swap per pass
# If no swaps were made, the string is sorted
if not swapped:
break
return total_time
The key idea is to analyze the string from right to left. For each '0', we need to find the earliest '1' to its right to swap with. By counting these swaps, we effectively determine the maximum time needed for the rearrangement.
Here's how the algorithm would work step-by-step:
def time_needed_to_rearrange(binary_string):
waiting_ones_count = 0
total_time = 0
maximum_time = 0
# Iterate backwards through the string.
for i in range(len(binary_string) - 1, -1, -1):
if binary_string[i] == '1':
waiting_ones_count += 1
# Count time based on waiting ones.
elif binary_string[i] == '0':
total_time += waiting_ones_count
# Keep track of the maximum time.
maximum_time = max(maximum_time, total_time)
return maximum_time
Case | How to Handle |
---|---|
Empty string | Return 0, as no rearrangement is needed for an empty string. |
String with length 1 | Return 0, as a single character string has no '01' sequence. |
String with all '0's | Return 0, as a string with only '0's has no '01' sequence. |
String with all '1's | Return 0, as a string with only '1's has no '01' sequence. |
String with a large number of '01' sequences | The algorithm should efficiently handle a large number of swaps without exceeding time limits. |
String alternates '01' many times like '01010101' | Ensure that the swapping logic correctly handles alternating sequences. |
String with a single '0' followed by many '1's ('011111') | The algorithm should efficiently handle the rearrangement in one step. |
String with many '0's followed by a single '1' ('000001') | The algorithm should identify no '01' sequence exists and return 0 |