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 <= 104s[i] is either '0' or '1'.Problem Overview: You are given a binary string s. The goal is to determine the minimum number of character flips required to transform it into an alternating string. An alternating binary string has no two adjacent characters equal, meaning it must follow either the pattern 010101... or 101010....
The key observation: only two valid alternating patterns exist for any length. If you compare the input string against both patterns and count mismatches, the smaller mismatch count gives the minimum number of flips needed.
Approach 1: Compare to Two Patterns (O(n) time, O(1) space)
Iterate through the string once while comparing each character with the expected character for two possible patterns: one starting with '0' (0101...) and one starting with '1' (1010...). For index i, the expected character depends on parity: even indices match the starting bit, odd indices match the opposite bit. Maintain two counters for mismatches—one for each pattern. Each time s[i] differs from the expected character, increment the corresponding counter. After scanning the entire string, return the smaller mismatch count.
This approach uses a simple string traversal with constant bookkeeping. No additional data structures are required. The algorithm runs in O(n) time because each character is processed exactly once, and it uses O(1) space since only two counters are maintained. This is the most common solution in interviews and production code because it is both minimal and easy to reason about.
Approach 2: Precomputed Masks (O(n) time, O(n) space)
Another way to solve the problem is to explicitly build the two target alternating strings of the same length as s. Construct one mask mask1 = "0101..." and another mask2 = "1010...". Then iterate through the original string and count the number of positions where s[i] differs from each mask. The minimum mismatch count between the two masks represents the number of flips needed.
This approach still performs a single pass over the string, giving O(n) time complexity. However, it allocates additional memory for the two mask strings, resulting in O(n) space complexity. The logic is straightforward and sometimes easier for beginners because the expected pattern is explicitly represented rather than computed dynamically.
Recommended for interviews: The two-pattern comparison approach is the expected solution. It demonstrates strong reasoning about string patterns and avoids unnecessary memory allocation. The mask approach shows correct thinking but adds extra space without improving runtime. Interviewers typically look for the constant-space scan because it proves you recognized that only two alternating patterns are possible.
The aim here is to transform the given string into either "010101..." or "101010...", as these are the only two possible alternating patterns for a binary string. By comparing the given string to these patterns, we can determine the minimum number of operations required.
We create these patterns for the length of the string and then count mismatches for both patterns by iterating through the string.
The minOperations function calculates the number of operations needed to make the string alternating by comparing it to two alternating patterns: 010101... and 101010.... It iterates through the string and counts the mismatches for both patterns, returning the minimum of the two counts.
Time Complexity: O(n), where n is the length of the string, due to the single pass through the string.
Space Complexity: O(1), as only a constant amount of extra space is used.
This method involves precomputing two mask strings that represent the possible alternating strings for any given length. We perform a comparison between the string and both masks, incrementing a counter for each mismatch, and outputting the smaller of the two.
In this solution, two mask strings are created to represent the two possible alternating patterns. The given string is then compared to each mask to count the number of changes required, with the minimum count being returned.
Time Complexity: O(n).
Space Complexity: O(n) for mask storage.
| Approach | Complexity |
|---|---|
| Compare to Two Patterns | Time Complexity: O(n), where n is the length of the string, due to the single pass through the string. |
| Precomputed Masks | Time Complexity: O(n). |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Compare to Two Patterns | O(n) | O(1) | Best general solution. Single pass with constant memory. |
| Precomputed Masks | O(n) | O(n) | Useful for clarity when explicitly comparing with target patterns. |
Minimum Changes To Make Alternating Binary String - Leetcode 1758 - Python • NeetCodeIO • 15,750 views views
Watch 9 more video solutions →Practice Minimum Changes To Make Alternating Binary String with our built-in code editor and test cases.
Practice on FleetCode