Problem · String

Minimum Operations to Make Alternating Binary String

EasyIBMINTERNOA
See IBM hiring insights

You are given a binary string. Transform it into an alternating binary string using the following operation: select any bit and flip it (change 0 to 1 or 1 to 0).

Find the minimum number of operations required to make the string alternating.

Examples
01 · Example 1
s = "11101"
return = 1

Flipping the bit at index 1 (0-based indexing) makes the string "10101", which is alternating. So the minimum operations is 1.

02 · Example 2
s = "111101"
return = 2

Flip the 1st and 3rd characters (1-based indexing) to obtain "101010", which is alternating. Thus, the minimum operations is 2.

Constraints
  • 0 ≤ length of s ≤ 2 * 10^5
  • s consists only of characters '0' and '1'
More IBM problems
drafts saved locally
public int solveBinaryString(String s) {
  // write your code here
}
s"11101"
expected1
sign in to submit