Skip to main content

Minimum Flips to Make Binary String Coherent - Solution & Explanation

MediumString3 min readAsked at: Amazon
Practice this problem

Problem Statement

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 <= 105
  • s[i] is either '0' or '1'.

Approach Overview

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

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Split PointO(n^2)O(1)Understanding the basic idea of testing every possible boundary
Prefix Count OptimizationO(n)O(n)When quick prefix lookups simplify flip counting across many split points
Greedy Single PassO(n)O(1)Optimal interview solution with minimal memory and one traversal

Video Solution

Leetcode 3922 | Minimum Flips to Make Binary String Coherent | Leetcode biweekly contest 182 • CodeWithMeGuys • 496 views views

Watch 3 more video solutions →

Frequently Asked Questions

Is Minimum Flips to Make Binary String Coherent easy or hard?
The problem is typically classified as Medium because the brute-force idea is straightforward but the optimal O(n) greedy transition is less obvious. Candidates must recognize that only boundary violations matter and update the minimum flips dynamically during traversal.
Minimum Flips to Make Binary String Coherent Python/Java solution
Implement a linear scan that tracks ones_seen and flips. For each character, increment ones_seen when you see '1'. When you encounter '0', update flips = min(flips + 1, ones_seen). The same logic translates directly to Python, Java, or C++ because it uses only primitive variables and a single loop.
How to solve Minimum Flips to Make Binary String Coherent in O(n)?
Perform a single left-to-right scan. Track the number of 1s seen and the minimum flips needed so far. When encountering a 0 after 1s, update flips using min(previous_flips + 1, ones_seen). This dynamic decision ensures the string remains monotonic while minimizing flips.
What is the best approach for Minimum Flips to Make Binary String Coherent?
The greedy single-pass approach is the most efficient. It scans the string once while maintaining the number of 1s seen and the minimum flips required so far. Each time a conflicting character appears, the algorithm decides whether flipping the current bit or previous bits is cheaper. This achieves O(n) time and O(1) space.
Is Minimum Flips to Make Binary String Coherent asked at Google/Amazon/Meta?
Binary string transformation problems with flip minimization frequently appear in interviews at large tech companies such as Google, Amazon, and Meta. Variants include making strings monotonic, minimizing bit changes, or enforcing ordering constraints using greedy or dynamic programming techniques.
What data structure is used in Minimum Flips to Make Binary String Coherent?
The optimal solution does not require complex data structures. It relies on simple counters and sequential array/string traversal. Some implementations use prefix arrays for counting zeros and ones, while the most optimized version maintains constant-space counters.
What is the time complexity of Minimum Flips to Make Binary String Coherent?
The optimal solution runs in O(n) time because it processes each character exactly once. Only a few counters are updated during the scan, so the space complexity remains O(1). Brute force approaches that test every split can take O(n^2) time.

Ready to solve this problem?

Practice Minimum Flips to Make Binary String Coherent with our built-in code editor and test cases.

Practice on FleetCode