Mid/SeniorString

Minimum Flips to Make Binary String Coherent

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 operations required to make s coherent.

Example 1
Inputs = "1010"
Output1
Flip s[0] to get "0010", which contains no "011" or "110" subsequences.
Example 2
Inputs = "0110"
Output1
Flip s[1] to get "0010", removing all forbidden subsequences "011" and "110".

Constraints

  • 1 <= s.length <= 10^5
  • s[i] is either '0' or '1'.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate