|
| 1 | +# [Problem 1888: Minimum Number of Flips to Make the Binary String Alternating](https://leetcode.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +We can perform arbitrary rotations (taking first char to end) and flip arbitrary characters. Rotations allow us to consider every cyclic shift of s; for each rotation we want the minimum flips required to make it alternating. A brute force that rotates and counts flips for each rotation would be O(n^2) if done naively. |
| 5 | + |
| 6 | +Observations: |
| 7 | +- An alternating string has only two possible patterns for a given length: "0101..." (start with '0') or "1010..." (start with '1'). |
| 8 | +- Considering rotations is equivalent to checking all length-n substrings of the doubled string s+s. |
| 9 | +- So if we build s2 = s + s and slide a window of length n across s2, we can count mismatches to the two alternating templates for each window in O(1) amortized time using a sliding-window mismatch count. |
| 10 | + |
| 11 | +This suggests an O(n) time, O(1) extra space solution. |
| 12 | + |
| 13 | +## Refining the problem, round 2 thoughts |
| 14 | +Edge cases: |
| 15 | +- n = 1: string is already alternating, answer is 0. |
| 16 | +- The sliding-window approach must carefully add the contribution of the incoming character and remove the outgoing one (when window exceeds size n). |
| 17 | +- For each index i in s2 we can determine the expected char for both templates by parity: for template0 (start with '0') expected is '0' if i%2==0 else '1'; for template1 it's the opposite. |
| 18 | +- Maintain two counters: mismatches to template0 and mismatches to template1 in the current window. When the window size reaches n, update the answer with the minimum of the two counters. |
| 19 | +- Complexity: O(n) time (we iterate over 2n chars once, with O(1) updates each step) and O(1) extra space (only counters and indices). |
| 20 | + |
| 21 | +Alternative solutions: |
| 22 | +- For even n there's a small optimization: rotation parity doesn't change counts in some way, but the sliding-window approach is simple and uniform for both even and odd n. |
| 23 | + |
| 24 | +## Attempted solution(s) |
| 25 | +```python |
| 26 | +class Solution: |
| 27 | + def minFlips(self, s: str) -> int: |
| 28 | + n = len(s) |
| 29 | + # If length 1, already alternating |
| 30 | + if n == 1: |
| 31 | + return 0 |
| 32 | + |
| 33 | + s2 = s + s |
| 34 | + # mismatch counts for templates starting with '0' and starting with '1' |
| 35 | + mismatch0 = 0 # mismatches to pattern "0101..." |
| 36 | + mismatch1 = 0 # mismatches to pattern "1010..." |
| 37 | + ans = float('inf') |
| 38 | + |
| 39 | + for i, ch in enumerate(s2): |
| 40 | + # expected characters for index i for both patterns |
| 41 | + if i % 2 == 0: |
| 42 | + expected0 = '0' |
| 43 | + expected1 = '1' |
| 44 | + else: |
| 45 | + expected0 = '1' |
| 46 | + expected1 = '0' |
| 47 | + |
| 48 | + if ch != expected0: |
| 49 | + mismatch0 += 1 |
| 50 | + if ch != expected1: |
| 51 | + mismatch1 += 1 |
| 52 | + |
| 53 | + # once we have more than n characters in window, remove the leftmost |
| 54 | + if i >= n: |
| 55 | + left = s2[i - n] |
| 56 | + j = i - n |
| 57 | + # expected chars at position j |
| 58 | + if j % 2 == 0: |
| 59 | + left_expected0 = '0' |
| 60 | + left_expected1 = '1' |
| 61 | + else: |
| 62 | + left_expected0 = '1' |
| 63 | + left_expected1 = '0' |
| 64 | + |
| 65 | + if left != left_expected0: |
| 66 | + mismatch0 -= 1 |
| 67 | + if left != left_expected1: |
| 68 | + mismatch1 -= 1 |
| 69 | + |
| 70 | + # when window size is exactly n, update answer |
| 71 | + if i >= n - 1: |
| 72 | + ans = min(ans, mismatch0, mismatch1) |
| 73 | + |
| 74 | + return ans if ans != float('inf') else 0 |
| 75 | +``` |
| 76 | +- Approach: Duplicate the string to consider all rotations as length-n windows of s+s. Maintain mismatch counts to the two alternating templates while sliding a window of size n. For each window compute the minimal flips needed (the fewer mismatches between the two templates). |
| 77 | +- Time complexity: O(n) — we scan 2n characters and perform O(1) work per character. |
| 78 | +- Space complexity: O(1) extra space (only counters and indices, plus the doubled string s2 which is O(n) but can be considered input-expanded; if strict extra-space counting excludes s2, then O(1)). |
| 79 | +- Important detail: computing expected characters by parity avoids building full pattern strings and keeps checks constant-time. |
0 commit comments