Problem · String
Minimum Operations to Make Alternating Binary String
Learn this problemProblem statement
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.
Function
solveBinaryString(s: String) → intExamples
Example 1
s = "11101"return = 1Flipping the bit at index 1 (0-based indexing) makes the string "10101", which is alternating. So the minimum operations is 1.
Example 2
s = "111101"return = 2Flip 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
- Parent Process NumberOA · Seen Jul 2026
- Request Retry CountOA · Seen Jul 2026
- Count Strictly Increasing Subsequences of Length 3OA · Seen Jul 2026
- Maximum Requests in a Time WindowOA · Seen Jul 2026
- Query Type Frequency WindowOA · Seen Jul 2026
- Minimum Number of Non-Empty Disjoint SegmentsOA · Seen Jul 2026
- Spam Text ClassificationOA · Seen Jul 2026
- Count Ideal NumbersOA · Seen Jun 2026