|
| 1 | +--- |
| 2 | +id: count-binary-substrings |
| 3 | +title: Count Binary Substrings |
| 4 | +sidebar_label: 0696 - Count Binary Substrings |
| 5 | +tags: |
| 6 | + - String |
| 7 | + - Two Pointers |
| 8 | + - Sliding Window |
| 9 | +description: "This is a solution to the Count Binary Substrings problem on LeetCode." |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +Given a binary string `s`, return the number of non-empty substrings that have the same number of `0`'s and `1`'s, and all the `0`'s and all the `1`'s in these substrings are grouped consecutively. |
| 15 | + |
| 16 | +Substrings that occur multiple times are counted the number of times they occur. |
| 17 | + |
| 18 | +### Examples |
| 19 | + |
| 20 | +**Example 1:** |
| 21 | + |
| 22 | +``` |
| 23 | +Input: s = "00110011" |
| 24 | +Output: 6 |
| 25 | +Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01". |
| 26 | +Notice that some of these substrings repeat and are counted the number of times they occur. |
| 27 | +Also, "00110011" is not a valid substring because all the 0's (and 1's) are not grouped together. |
| 28 | +``` |
| 29 | + |
| 30 | +**Example 2:** |
| 31 | + |
| 32 | +``` |
| 33 | +Input: s = "10101" |
| 34 | +Output: 4 |
| 35 | +Explanation: There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1's and 0's. |
| 36 | +``` |
| 37 | + |
| 38 | +### Constraints |
| 39 | + |
| 40 | +- $1 <= s.length <= 10^5$ |
| 41 | +- `s[i]` is either `'0'` or `'1'`. |
| 42 | + |
| 43 | +## Solution for Count Binary Substrings |
| 44 | + |
| 45 | +### Approach 1: Group By Character |
| 46 | +#### Intuition |
| 47 | + |
| 48 | +We can convert the string `s` into an array groups that represents the length of same-character contiguous blocks within the string. For example, if `s = "110001111000000"`, then `groups = [2, 3, 4, 6]`. |
| 49 | + |
| 50 | +For every binary string of the form `'0' * k + '1' * k` or `'1' * k + '0' * k`, the middle of this string must occur between two groups. |
| 51 | + |
| 52 | +Let's try to count the number of valid binary strings between `groups[i]` and `groups[i+1]`. If we have `groups[i] = 2, groups[i+1] = 3`, then it represents either "00111" or "11000". We clearly can make `min(groups[i], groups[i+1])` valid binary strings within this string. Because the binary digits to the left or right of this string must change at the boundary, our answer can never be larger. |
| 53 | + |
| 54 | +#### Algorithm |
| 55 | + |
| 56 | +Let's create `groups` as defined above. The first element of `s` belongs in its own group. From then on, each element either doesn't match the previous element, so that it starts a new group of size 1, or it does match, so that the size of the most recent group increases by 1. |
| 57 | + |
| 58 | +Afterward, we will take the sum of `min(groups[i-1], groups[i])`. |
| 59 | + |
| 60 | +## Code in Different Languages |
| 61 | + |
| 62 | +<Tabs> |
| 63 | +<TabItem value="java" label="Java"> |
| 64 | + <SolutionAuthor name="@Shreyash3087"/> |
| 65 | + |
| 66 | +```java |
| 67 | +class Solution { |
| 68 | + public int countBinarySubstrings(String s) { |
| 69 | + int[] groups = new int[s.length()]; |
| 70 | + int t = 0; |
| 71 | + groups[0] = 1; |
| 72 | + for (int i = 1; i < s.length(); i++) { |
| 73 | + if (s.charAt(i-1) != s.charAt(i)) { |
| 74 | + groups[++t] = 1; |
| 75 | + } else { |
| 76 | + groups[t]++; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + int ans = 0; |
| 81 | + for (int i = 1; i <= t; i++) { |
| 82 | + ans += Math.min(groups[i-1], groups[i]); |
| 83 | + } |
| 84 | + return ans; |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +</TabItem> |
| 90 | +<TabItem value="python" label="Python"> |
| 91 | + <SolutionAuthor name="@Shreyash3087"/> |
| 92 | + |
| 93 | +```python |
| 94 | +class Solution(object): |
| 95 | + def countBinarySubstrings(self, s): |
| 96 | + groups = [1] |
| 97 | + for i in xrange(1, len(s)): |
| 98 | + if s[i-1] != s[i]: |
| 99 | + groups.append(1) |
| 100 | + else: |
| 101 | + groups[-1] += 1 |
| 102 | + |
| 103 | + ans = 0 |
| 104 | + for i in xrange(1, len(groups)): |
| 105 | + ans += min(groups[i-1], groups[i]) |
| 106 | + return ans |
| 107 | +``` |
| 108 | +</TabItem> |
| 109 | +</Tabs> |
| 110 | + |
| 111 | +## Complexity Analysis |
| 112 | + |
| 113 | +### Time Complexity: $O(N)$ |
| 114 | + |
| 115 | +> **Reason**: where N is the length of s. Every loop is through $O(N)$ items with $O(1)$ work inside the for-block. |
| 116 | +
|
| 117 | +### Space Complexity: $O(N)$ |
| 118 | + |
| 119 | +> **Reason**: the space used by `groups`. |
| 120 | +
|
| 121 | +### Approach 2: Linear Scan |
| 122 | +#### Intuition and Algorithm |
| 123 | + |
| 124 | +We can amend our Approach #1 to calculate the answer on the fly. Instead of storing `groups`, we will remember only `prev = groups[-2]` and `cur = groups[-1]`. Then, the answer is the sum of `min(prev, cur)` over each different final `(prev, cur)` we see. |
| 125 | + |
| 126 | +## Code in Different Languages |
| 127 | + |
| 128 | +<Tabs> |
| 129 | +<TabItem value="java" label="Java"> |
| 130 | + <SolutionAuthor name="@Shreyash3087"/> |
| 131 | + |
| 132 | +```java |
| 133 | +class Solution { |
| 134 | + public int countBinarySubstrings(String s) { |
| 135 | + int ans = 0, prev = 0, cur = 1; |
| 136 | + for (int i = 1; i < s.length(); i++) { |
| 137 | + if (s.charAt(i-1) != s.charAt(i)) { |
| 138 | + ans += Math.min(prev, cur); |
| 139 | + prev = cur; |
| 140 | + cur = 1; |
| 141 | + } else { |
| 142 | + cur++; |
| 143 | + } |
| 144 | + } |
| 145 | + return ans + Math.min(prev, cur); |
| 146 | + } |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +</TabItem> |
| 151 | +<TabItem value="python" label="Python"> |
| 152 | + <SolutionAuthor name="@Shreyash3087"/> |
| 153 | + |
| 154 | +```python |
| 155 | +class Solution(object): |
| 156 | + def countBinarySubstrings(self, s): |
| 157 | + ans, prev, cur = 0, 0, 1 |
| 158 | + for i in xrange(1, len(s)): |
| 159 | + if s[i-1] != s[i]: |
| 160 | + ans += min(prev, cur) |
| 161 | + prev, cur = cur, 1 |
| 162 | + else: |
| 163 | + cur += 1 |
| 164 | + |
| 165 | + return ans + min(prev, cur) |
| 166 | +``` |
| 167 | +</TabItem> |
| 168 | +</Tabs> |
| 169 | + |
| 170 | +## Complexity Analysis |
| 171 | + |
| 172 | +### Time Complexity: $O(N)$ |
| 173 | + |
| 174 | +> **Reason**: where N is the length of s. Every loop is through $O(N)$ items with $O(1)$ work inside the for-block. |
| 175 | +
|
| 176 | +### Space Complexity: $O(1)$ |
| 177 | + |
| 178 | +> **Reason**: the space used by `prev`, `cur`, and `ans`. |
| 179 | +
|
| 180 | +## References |
| 181 | + |
| 182 | +- **LeetCode Problem**: [Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings/description/) |
| 183 | + |
| 184 | +- **Solution Link**: [Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings/solutions/) |
0 commit comments