You are given a string s
consisting only of the characters '0'
and '1'
. In one operation, you can change any '0'
to '1'
or vice versa.
The string is called alternating if no two adjacent characters are equal. For example, the string "010"
is alternating, while the string "0100"
is not.
Return the minimum number of operations needed to make s
alternating.
Example 1:
Input: s = "0100" Output: 1 Explanation: If you change the last character to '1', s will be "0101", which is alternating.
Example 2:
Input: s = "10" Output: 0 Explanation: s is already alternating.
Example 3:
Input: s = "1111" Output: 2 Explanation: You need two operations to reach "0101" or "1010".
Constraints:
1 <= s.length <= 104
s[i]
is either '0'
or '1'
.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 approach involves checking every single possible way to make the string alternate. We try forcing the string to start with a '0', then we try forcing it to start with a '1', and see which one requires fewer changes overall.
Here's how the algorithm would work step-by-step:
def min_operations_to_alternating_binary_string(string):
string_length = len(string)
start_with_zero_changes = 0
start_with_one_changes = 0
# Calculate changes if string starts with '0'
for index in range(string_length):
if index % 2 == 0:
if string[index] == '1':
start_with_zero_changes += 1
else:
if string[index] == '0':
start_with_zero_changes += 1
# Calculate changes if string starts with '1'
# We need to check both '0' and '1' starting patterns
for index in range(string_length):
if index % 2 == 0:
if string[index] == '0':
start_with_one_changes += 1
else:
if string[index] == '1':
start_with_one_changes += 1
# Choose the minimum number of changes between the two patterns
minimum_changes = min(start_with_zero_changes, start_with_one_changes)
return minimum_changes
The goal is to find the fewest changes needed to make a string alternate between 0 and 1. Instead of exploring all possibilities, we will consider two possible alternating patterns and count the differences from the given string for each pattern. The pattern with the fewest differences will be the optimal solution.
Here's how the algorithm would work step-by-step:
def min_operations_to_alternate(input_string):
string_length = len(input_string)
# Count differences for 0-start pattern.
zero_start_diff_count = 0
for i in range(string_length):
if i % 2 == 0:
if input_string[i] == '1':
zero_start_diff_count += 1
else:
if input_string[i] == '0':
zero_start_diff_count += 1
# Count differences for 1-start pattern.
one_start_diff_count = 0
for i in range(string_length):
if i % 2 == 0:
if input_string[i] == '0':
one_start_diff_count += 1
else:
if input_string[i] == '1':
one_start_diff_count += 1
# Return the minimum number of operations.
return min(zero_start_diff_count, one_start_diff_count)
Case | How to Handle |
---|---|
Empty string | Return 0 as no changes are needed to make an empty string alternating. |
String of length 1 | Return 0 as a single character string is already alternating. |
String with all '0's | The solution should correctly calculate the number of changes needed to make it '010101...' or '101010...' and choose the minimum. |
String with all '1's | The solution should correctly calculate the number of changes needed to make it '010101...' or '101010...' and choose the minimum. |
Maximum length string (consider memory/performance) | The solution should be O(n) in time and O(1) in space, so should scale linearly with the length of the string within reasonable memory limits. |
String is already alternating ('010101...') | The solution should correctly return 0 as no changes are needed. |
String is already alternating ('101010...') | The solution should correctly return 0 as no changes are needed. |
String length is even vs. odd | The code should handle both even and odd length strings correctly when comparing against '0101...' and '1010...' alternating patterns. |