|
| 1 | +# [1375.Number of Times Binary String Is Prefix-Aligned][title] |
| 2 | + |
| 3 | +## Description |
| 4 | +You have a **1-indexed** binary string of length `n` where all the bits are `0` initially. We will flip all the bits of this binary string (i.e., change them from `0` to `1`) one by one. You are given a **1-indexed** integer array `flips` where `flips[i]` indicates that the bit at index `i` will be flipped in the `ith` step. |
| 5 | + |
| 6 | +A binary string is **prefix-aligned** if, after the `ith` step, all the bits in the **inclusive** range `[1, i]` are ones and all the other bits are zeros. |
| 7 | + |
| 8 | +Return the number of times the binary string is **prefix-aligned** during the flipping process. |
| 9 | + |
| 10 | +**Example 1:** |
| 11 | + |
| 12 | +``` |
| 13 | +Input: flips = [3,2,4,1,5] |
| 14 | +Output: 2 |
| 15 | +Explanation: The binary string is initially "00000". |
| 16 | +After applying step 1: The string becomes "00100", which is not prefix-aligned. |
| 17 | +After applying step 2: The string becomes "01100", which is not prefix-aligned. |
| 18 | +After applying step 3: The string becomes "01110", which is not prefix-aligned. |
| 19 | +After applying step 4: The string becomes "11110", which is prefix-aligned. |
| 20 | +After applying step 5: The string becomes "11111", which is prefix-aligned. |
| 21 | +We can see that the string was prefix-aligned 2 times, so we return 2. |
| 22 | +``` |
| 23 | + |
| 24 | +**Example 2:** |
| 25 | + |
| 26 | +``` |
| 27 | +Input: flips = [4,1,2,3] |
| 28 | +Output: 1 |
| 29 | +Explanation: The binary string is initially "0000". |
| 30 | +After applying step 1: The string becomes "0001", which is not prefix-aligned. |
| 31 | +After applying step 2: The string becomes "1001", which is not prefix-aligned. |
| 32 | +After applying step 3: The string becomes "1101", which is not prefix-aligned. |
| 33 | +After applying step 4: The string becomes "1111", which is prefix-aligned. |
| 34 | +We can see that the string was prefix-aligned 1 time, so we return 1. |
| 35 | +``` |
| 36 | + |
| 37 | +## 结语 |
| 38 | + |
| 39 | +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] |
| 40 | + |
| 41 | +[title]: https://leetcode.com/problems/number-of-times-binary-string-is-prefix-aligned |
| 42 | +[me]: https://github.com/kylesliu/awesome-golang-algorithm |
0 commit comments