Watch 10 video solutions for Minimum Number of Flips to Make the Binary String Alternating, a medium level problem involving String, Dynamic Programming, Greedy. This walkthrough by NeetCode has 55,951 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a binary string s. You are allowed to perform two types of operations on the string in any sequence:
s and append it to the end of the string.s and flip its value, i.e., if its value is '0' it becomes '1' and vice-versa.Return the minimum number of type-2 operations you need to perform such that s becomes alternating.
The string is called alternating if no two adjacent characters are equal.
"010" and "1010" are alternating, while the string "0100" is not.
Example 1:
Input: s = "111000" Output: 2 Explanation: Use the first operation two times to make s = "100011". Then, use the second operation on the third and sixth elements to make s = "101010".
Example 2:
Input: s = "010" Output: 0 Explanation: The string is already alternating.
Example 3:
Input: s = "1110" Output: 1 Explanation: Use the second operation on the second element to make s = "1010".
Constraints:
1 <= s.length <= 105s[i] is either '0' or '1'.Problem Overview: You get a binary string s. You may rotate the string any number of times (move the first character to the end). After choosing a rotation, flip the minimum number of bits so the string becomes alternating (0101... or 1010...). Return the minimum flips required.
Approach 1: Greedy Pattern Comparison (O(n) time, O(1) space)
An alternating binary string can only follow two valid patterns: starting with 0 (0101...) or starting with 1 (1010...). Iterate through the string once and count mismatches against both patterns. Each mismatch represents a flip. The minimum of the two mismatch counts gives the answer if rotations were not allowed.
The challenge is that rotations change character positions. The greedy idea still helps: track mismatches relative to both patterns while scanning. This approach builds intuition about how alternating strings behave and shows why only two target patterns exist. Time complexity is O(n) and space complexity is O(1). This reasoning often appears in problems involving string pattern matching and greedy decisions.
Approach 2: Sliding Window with Rotations (O(n) time, O(n) space)
Rotations make the problem trickier because every rotation changes which indices should contain 0 or 1. Instead of physically rotating the string repeatedly, concatenate the string with itself (s + s). This simulates all rotations as contiguous substrings of length n.
Create two reference patterns of length 2n: one starting with 0 and the other starting with 1. Then slide a window of size n across the doubled string. Maintain mismatch counts between the window and each pattern. When the window grows beyond size n, subtract the contribution of the leftmost character. This is a classic sliding window technique.
For each window position, compute the flips needed for both alternating patterns and track the minimum. This effectively evaluates every rotation in linear time. The algorithm processes each character a constant number of times, giving O(n) time complexity and O(n) space for the doubled string and patterns.
Recommended for interviews: The sliding window with rotation simulation is the expected solution. Interviewers want to see the insight that rotations can be handled by doubling the string and evaluating length-n windows. Explaining the greedy mismatch counting first shows understanding of alternating patterns, but implementing the s + s sliding window demonstrates stronger algorithmic skill.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy Pattern Comparison | O(n) | O(1) | Useful for understanding alternating patterns when rotations are ignored or fixed |
| Sliding Window with Rotations | O(n) | O(n) | Best approach when rotations are allowed; evaluates all rotations efficiently |