You are given a binary string s.
A string is considered coherent if it does not contain "011" or "110" as subsequences.
In one operation, you can flip any character in s ('0' to '1' or '1' to '0').
Return an integer denoting the minimum number of modifications required to make s coherent.
A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
Example 1:
Input: s = "1010"
Output: 1
Explanation:
Flip s[0] to get "0010", which contains no "011" or "110" subsequences.
Example 2:
Input: s = "0110"
Output: 1
Explanation:
Flip s[1] to get "0010", removing all forbidden subsequences "011" and "110".
Example 3:
Input: s = "1000"
Output: 0
Explanation:
The string already has no "011" or "110" subsequences, so no flips are needed.
Constraints:
1 <= s.length <= 105s[i] is either '0' or '1'.Problem Overview: Given a binary string, determine the minimum number of bit flips required so the string becomes coherent. A coherent binary string has a consistent ordering: all 0s followed by 1s or all 1s followed by 0s. Each flip changes a single character (0 → 1 or 1 → 0).
Approach 1: Brute Force Split Point (O(n^2) time, O(1) space)
Try every possible split index that divides the string into two parts. For a 0...01...1 pattern, count how many 1s appear in the left portion (they must flip to 0) and how many 0s appear in the right portion (they must flip to 1). Repeat the same logic for the opposite pattern 1...10...0. Each split requires scanning both halves, giving O(n) work per split and O(n^2) total time. This approach demonstrates the core idea but becomes slow for large inputs.
Approach 2: Prefix Count Optimization (O(n) time, O(n) space)
Precompute prefix counts of zeros and ones so the number of flips for any split can be calculated in constant time. For example, if the string must follow 0...01...1, the cost at split i equals the number of 1s in the prefix plus the number of 0s in the suffix. Prefix arrays let you compute those counts instantly. Iterate through all split points and track the minimum flips. This turns the brute force scan into a linear pass using simple arithmetic over prefix data.
Approach 3: Greedy Single Pass (O(n) time, O(1) space)
The optimal approach scans the string once while tracking how many characters violate the desired ordering. Maintain a running count of 1s seen so far and the minimum flips required to maintain monotonic order. When a 0 appears after 1s, you either flip the current 0 or flip all previous 1s. Update the flip count with min(previous_flips + 1, ones_seen). A second pass can evaluate the reverse pattern if needed. This greedy state update avoids extra arrays and keeps space constant. The pattern is common in problems involving monotonic binary sequences and greedy algorithms combined with simple dynamic programming state transitions.
Recommended for interviews: The greedy single-pass solution. Interviewers expect you to recognize that only a boundary between groups matters and maintain minimal flips while scanning. Starting with the brute-force split shows you understand the structure, but reducing it to O(n) with constant space demonstrates stronger algorithmic reasoning and familiarity with array traversal patterns.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Split Point | O(n^2) | O(1) | Understanding the basic idea of testing every possible boundary |
| Prefix Count Optimization | O(n) | O(n) | When quick prefix lookups simplify flip counting across many split points |
| Greedy Single Pass | O(n) | O(1) | Optimal interview solution with minimal memory and one traversal |
Practice Minimum Flips to Make Binary String Coherent with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor