Problem Statement
Given a binary string s containing only 0 and 1, the task is to find the minimum number of bit flips required to make the string alternating.
A binary string is called alternating when no two adjacent characters are the same.
A bit flip means changing:
0to11to0
The goal is to make the string alternating using the minimum number of flips.
Examples
Example 1:
Input: s = "001"
Output: 1
Explanation: By flipping the character at index 0, the string becomes "101", which is alternating. Therefore, only one flip is required.
Example 2:
Input: s = "0001010111"
Output: 2
Explanation: By flipping the characters at indices 1 and 8, the string becomes "0101010101", which is alternating. Therefore, the minimum number of flips required is 2.
Approach: Compare Two Alternating Patterns
An alternating binary string can only follow one of two patterns:
010101010...101010101...
Instead of generating new strings, both patterns can be checked by comparing the expected character at each index with the actual character in the input string.
Two counters are maintained:
flip0– counts the number of mismatches when the expected pattern starts with0.flip1– counts the number of mismatches when the expected pattern starts with1.
At every index, the expected character for both patterns is calculated using the index parity (i % 2).
After traversing the string, the smaller of the two flip counts is returned.
Step-by-step algorithm
- Initialize two counters,
flip0andflip1, to0. - Traverse the string from index
0tos.Length - 1. - For the first pattern (
0101...):- At even indices, the expected character is
0. - At odd indices, the expected character is
1.
- At even indices, the expected character is
- For the second pattern (
1010...):- At even indices, the expected character is
1. - At odd indices, the expected character is
0.
- At even indices, the expected character is
- If the actual character doesn't match the expected character, increment the corresponding counter.
- Return
Math.Min(flip0, flip1).
C# Code
class Solution
{
public int minFlips(string s)
{
int flip0 = 0;
int flip1 = 0;
for (int i = 0; i < s.Length; i++)
{
char expected0 = (i % 2 == 0) ? '0' : '1';
char expected1 = (i % 2 == 0) ? '1' : '0';
if (s[i] != expected0)
flip0++;
if (s[i] != expected1)
flip1++;
}
return Math.Min(flip0, flip1);
}
}Code Explanation
1. Initialize the counters
int flip0 = 0;
int flip1 = 0;Two integer variables are created to count the required flips for the two possible alternating patterns.
flip0tracks flips for010101....flip1tracks flips for101010....
2. Traverse the string
for (int i = 0; i < s.Length; i++)The loop traverses each character of the string exactly once. The index i is used to determine whether the current position is even or odd.
3. Calculate the expected characters
char expected0 = (i % 2 == 0) ? '0' : '1';
char expected1 = (i % 2 == 0) ? '1' : '0';The modulo operator % is used to determine the parity of the index.
- If
i % 2 == 0, the index is even. - Otherwise, the index is odd.
The ternary operator ? : assigns the expected character based on the index.
| Index | Pattern 1 (0101...) | Pattern 2 (1010...) |
|---|---|---|
| 0 | 0 | 1 |
| 1 | 1 | 0 |
| 2 | 0 | 1 |
| 3 | 1 | 0 |
| 4 | 0 | 1 |
4. Count mismatches
if (s[i] != expected0)
flip0++;
if (s[i] != expected1)
flip1++;If the actual character doesn't match the expected character of a pattern, one flip is required for that position.
The counters are incremented independently for each pattern.
5. Return the minimum
return Math.Min(flip0, flip1);Math.Min() compares both flip counts and returns the smaller value, which represents the minimum number of flips required to make the string alternating.
Dry Run
Input: s = "001"
We compare the input string with both alternating patterns.
Pattern 1: 010
| Index | Actual | Expected | Flip? |
|---|---|---|---|
| 0 | 0 | 0 | No |
| 1 | 0 | 1 | Yes |
| 2 | 1 | 0 | Yes |
Total flips for Pattern 1 = 2
Pattern 2: 101
| Index | Actual | Expected | Flip? |
|---|---|---|---|
| 0 | 0 | 1 | Yes |
| 1 | 0 | 0 | No |
| 2 | 1 | 1 | No |
Total flips for Pattern 2 = 1
Therefore:
Minimum flips = Math.Min(2, 1)
Answer: 1Dry Run – Example 2
Input: s = "0001010111"
| Pattern | Alternating String | Mismatches |
|---|---|---|
| Pattern 1 | 0101010101 | 2 |
| Pattern 2 | 1010101010 | 8 |
The first pattern requires only two flips, whereas the second pattern requires eight flips.
Answer: 2
Why does this approach work?
Every alternating binary string must start with either 0 or 1. Once the starting character is selected, all the remaining characters are fixed because each character must be different from the previous one.
Therefore, there are exactly two possible alternating patterns for a given string length.
By counting the mismatches for both patterns and selecting the smaller count, the minimum number of flips is guaranteed to be found.
Complexity Analysis
- Time Complexity: O(n) – The string is traversed once, and each character is compared with the expected characters of both patterns. Here,
nis the length of the string. - Auxiliary Space: O(1) – Only a constant number of variables are used. No additional arrays or strings are created.
Key Takeaways
- An alternating binary string has only two possible patterns.
i % 2helps identify even and odd positions.- The ternary operator can determine expected characters efficiently.
Math.Min()selects the smaller number of flips.- The solution uses a single traversal with constant extra space.


Join the conversation! Your thoughts help the community grow.