Problem · String
Minimum Flips to Alternate Binary String (With K Flip Window)
Learn this problemProblem statement
You are given a binary string s. In one operation, you can flip a subarray of length exactly k (flip all bits in that subarray). Return the minimum number of such operations needed to make the string alternating (i.e., no two adjacent bits are the same). If it's not possible, return -1.
Function
minFlips(s: String, k: int) → intExamples
Example 1
s = "00010111"k = 3return = 6The target 01010101 is unreachable with length-3 flips. To reach 10101010, the leftmost mismatch forces flips starting at indices 0, 1, 2, 3, 4, and 5, for 6 operations. Exhaustive state search confirms that no shorter sequence exists.
Constraints
1 ≤ s.length ≤ 2 * 10^5s[i]is0or1.1 ≤ k ≤ s.length- Every operation flips exactly
kconsecutive bits.
More Google problems
- Deduplicate Logs: Keep FirstONSITE INTERVIEW · Seen Jul 2026
- Deduplicate Logs: Keep LatestONSITE INTERVIEW · Seen Jul 2026
- Find a Template Across Binary-Tree LeavesONSITE INTERVIEW · Seen Jul 2026
- Maximum Programmer-Problem MatchingONSITE INTERVIEW · Seen Jul 2026
- Minimum Direction ViolationsONSITE INTERVIEW · Seen Jul 2026
- Stream Latest Log VersionsONSITE INTERVIEW · Seen Jul 2026
- Stream Unique Logs in Timestamp OrderONSITE INTERVIEW · Seen Jul 2026
- Top-K IP Addresses from File RecordsONSITE INTERVIEW · Seen Jul 2026