Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6417a6c

Browse files
committed
feat: add solutions to lc problem: No.0696
No.0696.Count Binary Substrings
1 parent 6900c10 commit 6417a6c

File tree

6 files changed

+259
-26
lines changed

6 files changed

+259
-26
lines changed

‎solution/0600-0699/0696.Count Binary Substrings/README.md‎

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
<li><code>s</code> 只包含"0"或"1"字符。</li>
4242
</ul>
4343

44-
4544
## 解法
4645

4746
<!-- 这里可写通用的实现逻辑 -->
@@ -53,15 +52,104 @@
5352
<!-- 这里可写当前语言的特殊实现逻辑 -->
5453

5554
```python
56-
55+
class Solution:
56+
def countBinarySubstrings(self, s: str) -> int:
57+
i, n = 0, len(s)
58+
t = []
59+
while i < n:
60+
cnt = 1
61+
while i + 1 < n and s[i + 1] == s[i]:
62+
cnt += 1
63+
i += 1
64+
t.append(cnt)
65+
i += 1
66+
ans = 0
67+
for i in range(1, len(t)):
68+
ans += min(t[i - 1], t[i])
69+
return ans
5770
```
5871

5972
### **Java**
6073

6174
<!-- 这里可写当前语言的特殊实现逻辑 -->
6275

6376
```java
77+
class Solution {
78+
public int countBinarySubstrings(String s) {
79+
int i = 0, n = s.length();
80+
List<Integer> t = new ArrayList<>();
81+
while (i < n) {
82+
int cnt = 1;
83+
while (i + 1 < n && s.charAt(i + 1) == s.charAt(i)) {
84+
++i;
85+
++cnt;
86+
}
87+
t.add(cnt);
88+
++i;
89+
}
90+
int ans = 0;
91+
for (i = 1; i < t.size(); ++i) {
92+
ans += Math.min(t.get(i - 1), t.get(i));
93+
}
94+
return ans;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int countBinarySubstrings(string s) {
105+
int i = 0, n = s.size();
106+
vector<int> t;
107+
while (i < n)
108+
{
109+
int cnt = 1;
110+
while (i + 1 < n && s[i + 1] == s[i])
111+
{
112+
++cnt;
113+
++i;
114+
}
115+
t.push_back(cnt);
116+
++i;
117+
}
118+
int ans = 0;
119+
for (i = 1; i < t.size(); ++i) ans += min(t[i - 1], t[i]);
120+
return ans;
121+
}
122+
};
123+
```
64124
125+
### **Go**
126+
127+
```go
128+
func countBinarySubstrings(s string) int {
129+
i, n := 0, len(s)
130+
var t []int
131+
for i < n {
132+
cnt := 1
133+
for i+1 < n && s[i+1] == s[i] {
134+
i++
135+
cnt++
136+
}
137+
t = append(t, cnt)
138+
i++
139+
}
140+
ans := 0
141+
for i := 1; i < len(t); i++ {
142+
ans += min(t[i-1], t[i])
143+
}
144+
return ans
145+
}
146+
147+
func min(a, b int) int {
148+
if a < b {
149+
return a
150+
}
151+
return b
152+
}
65153
```
66154

67155
### **...**

‎solution/0600-0699/0696.Count Binary Substrings/README_EN.md‎

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44

55
## Description
66

7-
<p>Give a string <code>s</code>, count the number of non-empty (contiguous) 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.
7+
<p>Give a string <code>s</code>, count the number of non-empty (contiguous) 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.
88

99
</p>
1010

1111
<p>Substrings that occur multiple times are counted the number of times they occur.</p>
1212

13-
14-
1513
<p><b>Example 1:</b><br />
1614

1715
<pre>
@@ -30,8 +28,6 @@
3028

3129
</p>
3230

33-
34-
3531
<p><b>Example 2:</b><br />
3632

3733
<pre>
@@ -46,8 +42,6 @@
4642

4743
</p>
4844

49-
50-
5145
<p><b>Note:</b>
5246

5347
<li><code>s.length</code> will be between 1 and 50,000.</li>
@@ -63,13 +57,102 @@
6357
### **Python3**
6458

6559
```python
66-
60+
class Solution:
61+
def countBinarySubstrings(self, s: str) -> int:
62+
i, n = 0, len(s)
63+
t = []
64+
while i < n:
65+
cnt = 1
66+
while i + 1 < n and s[i + 1] == s[i]:
67+
cnt += 1
68+
i += 1
69+
t.append(cnt)
70+
i += 1
71+
ans = 0
72+
for i in range(1, len(t)):
73+
ans += min(t[i - 1], t[i])
74+
return ans
6775
```
6876

6977
### **Java**
7078

7179
```java
80+
class Solution {
81+
public int countBinarySubstrings(String s) {
82+
int i = 0, n = s.length();
83+
List<Integer> t = new ArrayList<>();
84+
while (i < n) {
85+
int cnt = 1;
86+
while (i + 1 < n && s.charAt(i + 1) == s.charAt(i)) {
87+
++i;
88+
++cnt;
89+
}
90+
t.add(cnt);
91+
++i;
92+
}
93+
int ans = 0;
94+
for (i = 1; i < t.size(); ++i) {
95+
ans += Math.min(t.get(i - 1), t.get(i));
96+
}
97+
return ans;
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int countBinarySubstrings(string s) {
108+
int i = 0, n = s.size();
109+
vector<int> t;
110+
while (i < n)
111+
{
112+
int cnt = 1;
113+
while (i + 1 < n && s[i + 1] == s[i])
114+
{
115+
++cnt;
116+
++i;
117+
}
118+
t.push_back(cnt);
119+
++i;
120+
}
121+
int ans = 0;
122+
for (i = 1; i < t.size(); ++i) ans += min(t[i - 1], t[i]);
123+
return ans;
124+
}
125+
};
126+
```
72127
128+
### **Go**
129+
130+
```go
131+
func countBinarySubstrings(s string) int {
132+
i, n := 0, len(s)
133+
var t []int
134+
for i < n {
135+
cnt := 1
136+
for i+1 < n && s[i+1] == s[i] {
137+
i++
138+
cnt++
139+
}
140+
t = append(t, cnt)
141+
i++
142+
}
143+
ans := 0
144+
for i := 1; i < len(t); i++ {
145+
ans += min(t[i-1], t[i])
146+
}
147+
return ans
148+
}
149+
150+
func min(a, b int) int {
151+
if a < b {
152+
return a
153+
}
154+
return b
155+
}
73156
```
74157

75158
### **...**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int countBinarySubstrings(string s) {
4+
int i = 0, n = s.size();
5+
vector<int> t;
6+
while (i < n)
7+
{
8+
int cnt = 1;
9+
while (i + 1 < n && s[i + 1] == s[i])
10+
{
11+
++cnt;
12+
++i;
13+
}
14+
t.push_back(cnt);
15+
++i;
16+
}
17+
int ans = 0;
18+
for (i = 1; i < t.size(); ++i) ans += min(t[i - 1], t[i]);
19+
return ans;
20+
}
21+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func countBinarySubstrings(s string) int {
2+
i, n := 0, len(s)
3+
var t []int
4+
for i < n {
5+
cnt := 1
6+
for i+1 < n && s[i+1] == s[i] {
7+
i++
8+
cnt++
9+
}
10+
t = append(t, cnt)
11+
i++
12+
}
13+
ans := 0
14+
for i := 1; i < len(t); i++ {
15+
ans += min(t[i-1], t[i])
16+
}
17+
return ans
18+
}
19+
20+
func min(a, b int) int {
21+
if a < b {
22+
return a
23+
}
24+
return b
25+
}
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
class Solution {
22
public int countBinarySubstrings(String s) {
3-
int[] group = new int[s.length()];
4-
int k = 0;
5-
Arrays.fill(group , 0);
6-
group[0] = 1;
7-
for(int i = 1;i < s.length();i++) {
8-
if(s.charAt(i) == s.charAt(i-1))
9-
group[k]++;
10-
else
11-
group[++k] = 1;
12-
}
13-
int ans = 0;
14-
for(int i = 1;i < s.length() && group[i] != 0;i++) {
15-
ans += Math.min(group[i-1], group[i]);
16-
}
17-
return ans;
3+
int i = 0, n = s.length();
4+
List<Integer> t = new ArrayList<>();
5+
while (i < n) {
6+
int cnt = 1;
7+
while (i + 1 < n && s.charAt(i + 1) == s.charAt(i)) {
8+
++i;
9+
++cnt;
10+
}
11+
t.add(cnt);
12+
++i;
13+
}
14+
int ans = 0;
15+
for (i = 1; i < t.size(); ++i) {
16+
ans += Math.min(t.get(i - 1), t.get(i));
17+
}
18+
return ans;
1819
}
19-
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def countBinarySubstrings(self, s: str) -> int:
3+
i, n = 0, len(s)
4+
t = []
5+
while i < n:
6+
cnt = 1
7+
while i + 1 < n and s[i + 1] == s[i]:
8+
cnt += 1
9+
i += 1
10+
t.append(cnt)
11+
i += 1
12+
ans = 0
13+
for i in range(1, len(t)):
14+
ans += min(t[i - 1], t[i])
15+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /