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^5sconsists only of characters '0' and '1'
More IBM problems
- Count Descending SubarraysOA · Seen Apr 2026
- Count Power Products in RangeOA · Seen Apr 2026
- Minimum Number of Non-Empty Disjoint SegmentsSeen Feb 2026
- Count Unstable ProcessesOA · Seen Feb 2026
- Longest Balanced Binary SubarrayOA · Seen Feb 2026
- Process Execution TimeOA · Seen Nov 2025
- Service Timeout DetectionOA · Seen Nov 2025
- Get Minimum OperationsSeen May 2025
public int solveBinaryString(String s) {
// write your code here
}
s"11101"
expected1
sign in to submit