Skip to content

Commit f31bc2a

Browse files
committedApr 2, 2023
feat: add solutions to lc problem: No.2609
No.2609.Find the Longest Balanced Substring of a Binary String
1 parent 0b6d489 commit f31bc2a

File tree

6 files changed

+243
-65
lines changed

6 files changed

+243
-65
lines changed
 

‎solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README.md

+109
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:暴力枚举**
57+
58+
注意到数据范围很小,因此,我们可以枚举所有的子串 $s[i..j]$,检查其是否为平衡子串,如果是,则更新答案。
59+
60+
时间复杂度 $O(n^3)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
61+
62+
**方法二:枚举优化**
63+
64+
我们用变量 $zero$ 和 $one$ 分别记录当前连续的 $0$ 和 $1$ 的个数。
65+
66+
遍历字符串 $s$,对于当前字符 $c$:
67+
68+
- 如果当前字符为 `'0'`,我们判断此时 $one$ 是否大于 $0$,是则将 $zero$ 和 $one$ 重置为 $0$,接下来将 $zero$ 加 $1$。
69+
- 如果当前字符为 `'1'`,则将 $one$ 加 $1$,并更新答案为 $2 \times min(one, zero)$。
70+
71+
遍历结束后,即可得到最长的平衡子串的长度。
72+
73+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
74+
5675
<!-- tabs:start -->
5776

5877
### **Python3**
@@ -80,6 +99,21 @@ class Solution:
8099
return ans
81100
```
82101

102+
```python
103+
class Solution:
104+
def findTheLongestBalancedSubstring(self, s: str) -> int:
105+
ans = zero = one = 0
106+
for c in s:
107+
if c == '0':
108+
if one:
109+
zero = one = 0
110+
zero += 1
111+
else:
112+
one += 1
113+
ans = max(ans, 2 * min(one, zero))
114+
return ans
115+
```
116+
83117
### **Java**
84118

85119
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -113,6 +147,27 @@ class Solution {
113147
}
114148
```
115149

150+
```java
151+
class Solution {
152+
public int findTheLongestBalancedSubstring(String s) {
153+
int zero = 0, one = 0;
154+
int ans = 0, n = s.length();
155+
for (int i = 0; i < n; ++i) {
156+
if (s.charAt(i) == '0') {
157+
if (one > 0) {
158+
zero = 0;
159+
one = 0;
160+
}
161+
++zero;
162+
} else {
163+
ans = Math.max(ans, 2 * Math.min(zero, ++one));
164+
}
165+
}
166+
return ans;
167+
}
168+
}
169+
```
170+
116171
### **C++**
117172

118173
```cpp
@@ -144,6 +199,28 @@ public:
144199
};
145200
```
146201
202+
```cpp
203+
class Solution {
204+
public:
205+
int findTheLongestBalancedSubstring(string s) {
206+
int zero = 0, one = 0;
207+
int ans = 0, n = s.size();
208+
for (char& c : s) {
209+
if (c == '0') {
210+
if (one > 0) {
211+
zero = 0;
212+
one = 0;
213+
}
214+
++zero;
215+
} else {
216+
ans = max(ans, 2 * min(zero, ++one));
217+
}
218+
}
219+
return ans;
220+
}
221+
};
222+
```
223+
147224
### **Go**
148225

149226
```go
@@ -178,6 +255,38 @@ func max(a, b int) int {
178255
}
179256
```
180257

258+
```go
259+
func findTheLongestBalancedSubstring(s string) (ans int) {
260+
zero, one := 0, 0
261+
for _, c := range s {
262+
if c == '0' {
263+
if one > 0 {
264+
zero, one = 0, 0
265+
}
266+
zero++
267+
} else {
268+
one++
269+
ans = max(ans, 2*min(zero, one))
270+
}
271+
}
272+
return
273+
}
274+
275+
func max(a, b int) int {
276+
if a > b {
277+
return a
278+
}
279+
return b
280+
}
281+
282+
func min(a, b int) int {
283+
if a < b {
284+
return a
285+
}
286+
return b
287+
}
288+
```
289+
181290
### **...**
182291

183292
```

‎solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README_EN.md

+90
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ class Solution:
7272
return ans
7373
```
7474

75+
```python
76+
class Solution:
77+
def findTheLongestBalancedSubstring(self, s: str) -> int:
78+
ans = zero = one = 0
79+
for c in s:
80+
if c == '0':
81+
if one:
82+
zero = one = 0
83+
zero += 1
84+
else:
85+
one += 1
86+
ans = max(ans, 2 * min(one, zero))
87+
return ans
88+
```
89+
7590
### **Java**
7691

7792
```java
@@ -103,6 +118,27 @@ class Solution {
103118
}
104119
```
105120

121+
```java
122+
class Solution {
123+
public int findTheLongestBalancedSubstring(String s) {
124+
int zero = 0, one = 0;
125+
int ans = 0, n = s.length();
126+
for (int i = 0; i < n; ++i) {
127+
if (s.charAt(i) == '0') {
128+
if (one > 0) {
129+
zero = 0;
130+
one = 0;
131+
}
132+
++zero;
133+
} else {
134+
ans = Math.max(ans, 2 * Math.min(zero, ++one));
135+
}
136+
}
137+
return ans;
138+
}
139+
}
140+
```
141+
106142
### **C++**
107143

108144
```cpp
@@ -134,6 +170,28 @@ public:
134170
};
135171
```
136172
173+
```cpp
174+
class Solution {
175+
public:
176+
int findTheLongestBalancedSubstring(string s) {
177+
int zero = 0, one = 0;
178+
int ans = 0, n = s.size();
179+
for (char& c : s) {
180+
if (c == '0') {
181+
if (one > 0) {
182+
zero = 0;
183+
one = 0;
184+
}
185+
++zero;
186+
} else {
187+
ans = max(ans, 2 * min(zero, ++one));
188+
}
189+
}
190+
return ans;
191+
}
192+
};
193+
```
194+
137195
### **Go**
138196

139197
```go
@@ -168,6 +226,38 @@ func max(a, b int) int {
168226
}
169227
```
170228

229+
```go
230+
func findTheLongestBalancedSubstring(s string) (ans int) {
231+
zero, one := 0, 0
232+
for _, c := range s {
233+
if c == '0' {
234+
if one > 0 {
235+
zero, one = 0, 0
236+
}
237+
zero++
238+
} else {
239+
one++
240+
ans = max(ans, 2*min(zero, one))
241+
}
242+
}
243+
return
244+
}
245+
246+
func max(a, b int) int {
247+
if a > b {
248+
return a
249+
}
250+
return b
251+
}
252+
253+
func min(a, b int) int {
254+
if a < b {
255+
return a
256+
}
257+
return b
258+
}
259+
```
260+
171261
### **...**
172262

173263
```

‎solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/Solution.cpp

+10-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
class Solution {
22
public:
33
int findTheLongestBalancedSubstring(string s) {
4-
int n = s.size();
5-
int ans = 0;
6-
auto check = [&](int i, int j) -> bool {
7-
int cnt = 0;
8-
for (int k = i; k <= j; ++k) {
9-
if (s[k] == '1') {
10-
++cnt;
11-
} else if (cnt) {
12-
return false;
13-
}
14-
}
15-
return cnt * 2 == j - i + 1;
16-
};
17-
for (int i = 0; i < n; ++i) {
18-
for (int j = i + 1; j < n; ++j) {
19-
if (check(i, j)) {
20-
ans = max(ans, j - i + 1);
4+
int zero = 0, one = 0;
5+
int ans = 0, n = s.size();
6+
for (char& c : s) {
7+
if (c == '0') {
8+
if (one > 0) {
9+
zero = 0;
10+
one = 0;
2111
}
12+
++zero;
13+
} else {
14+
ans = max(ans, 2 * min(zero, ++one));
2215
}
2316
}
2417
return ans;
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
func findTheLongestBalancedSubstring(s string) (ans int) {
2-
n := len(s)
3-
check := func(i, j int) bool {
4-
cnt := 0
5-
for k := i; k <= j; k++ {
6-
if s[k] == '1' {
7-
cnt++
8-
} else if cnt > 0 {
9-
return false
10-
}
11-
}
12-
return cnt*2 == j-i+1
13-
}
14-
for i := 0; i < n; i++ {
15-
for j := i + 1; j < n; j++ {
16-
if check(i, j) {
17-
ans = max(ans, j-i+1)
2+
zero, one := 0, 0
3+
for _, c := range s {
4+
if c == '0' {
5+
if one > 0 {
6+
zero, one = 0, 0
187
}
8+
zero++
9+
} else {
10+
one++
11+
ans = max(ans, 2*min(zero, one))
1912
}
2013
}
2114
return
@@ -26,4 +19,11 @@ func max(a, b int) int {
2619
return a
2720
}
2821
return b
22+
}
23+
24+
func min(a, b int) int {
25+
if a < b {
26+
return a
27+
}
28+
return b
2929
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
class Solution {
22
public int findTheLongestBalancedSubstring(String s) {
3-
int n = s.length();
4-
int ans = 0;
3+
int zero = 0, one = 0;
4+
int ans = 0, n = s.length();
55
for (int i = 0; i < n; ++i) {
6-
for (int j = i + 1; j < n; ++j) {
7-
if (check(s, i, j)) {
8-
ans = Math.max(ans, j - i + 1);
6+
if (s.charAt(i) == '0') {
7+
if (one > 0) {
8+
zero = 0;
9+
one = 0;
910
}
11+
++zero;
12+
} else {
13+
ans = Math.max(ans, 2 * Math.min(zero, ++one));
1014
}
1115
}
1216
return ans;
1317
}
14-
15-
private boolean check(String s, int i, int j) {
16-
int cnt = 0;
17-
for (int k = i; k <= j; ++k) {
18-
if (s.charAt(k) == '1') {
19-
++cnt;
20-
} else if (cnt > 0) {
21-
return false;
22-
}
23-
}
24-
return cnt * 2 == j - i + 1;
25-
}
2618
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
class Solution:
22
def findTheLongestBalancedSubstring(self, s: str) -> int:
3-
def check(i, j):
4-
cnt = 0
5-
for k in range(i, j + 1):
6-
if s[k] == '1':
7-
cnt += 1
8-
elif cnt:
9-
return False
10-
return cnt * 2 == (j - i + 1)
11-
12-
n = len(s)
13-
ans = 0
14-
for i in range(n):
15-
for j in range(i + 1, n):
16-
if check(i, j):
17-
ans = max(ans, j - i + 1)
3+
ans = zero = one = 0
4+
for c in s:
5+
if c == '0':
6+
if one:
7+
zero = one = 0
8+
zero += 1
9+
else:
10+
one += 1
11+
ans = max(ans, 2 * min(one, zero))
1812
return ans

0 commit comments

Comments
 (0)
Please sign in to comment.