Skip to content

Commit d1f1046

Browse files
authoredJul 6, 2023
feat: add rust solution to lc problem: No.2609 (#1155)
1 parent c7293b9 commit d1f1046

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed
 

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

+64
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,70 @@ function findTheLongestBalancedSubstring(s: string): number {
335335
}
336336
```
337337

338+
### **Rust**
339+
340+
```rust
341+
impl Solution {
342+
pub fn find_the_longest_balanced_substring(s: String) -> i32 {
343+
let check = |i: usize, j: usize| -> bool {
344+
let mut cnt = 0;
345+
346+
for k in i..=j {
347+
if s.as_bytes()[k] == b'1' {
348+
cnt += 1;
349+
} else if cnt > 0 {
350+
return false
351+
}
352+
}
353+
354+
cnt * 2 == j - i + 1
355+
};
356+
357+
let mut ans = 0;
358+
let n = s.len();
359+
for i in 0..n - 1 {
360+
for j in (i + 1..n).rev() {
361+
if j - i + 1 < ans {
362+
break;
363+
}
364+
365+
if check(i, j) {
366+
ans = std::cmp::max(ans, j - i + 1);
367+
break;
368+
}
369+
}
370+
}
371+
372+
ans as i32
373+
}
374+
}
375+
```
376+
377+
```rust
378+
impl Solution {
379+
pub fn find_the_longest_balanced_substring(s: String) -> i32 {
380+
let mut zero = 0;
381+
let mut one = 0;
382+
let mut ans = 0;
383+
384+
for &c in s.as_bytes().iter() {
385+
if c == b'0' {
386+
if one > 0 {
387+
zero = 0;
388+
one = 0;
389+
}
390+
zero += 1;
391+
} else {
392+
one += 1;
393+
ans = std::cmp::max(ans, std::cmp::min(zero, one) * 2)
394+
}
395+
}
396+
397+
ans
398+
}
399+
}
400+
```
401+
338402
### **...**
339403

340404
```

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

+64
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,70 @@ function findTheLongestBalancedSubstring(s: string): number {
325325
}
326326
```
327327

328+
### **Rust**
329+
330+
```rust
331+
impl Solution {
332+
pub fn find_the_longest_balanced_substring(s: String) -> i32 {
333+
let check = |i: usize, j: usize| -> bool {
334+
let mut cnt = 0;
335+
336+
for k in i..=j {
337+
if s.as_bytes()[k] == b'1' {
338+
cnt += 1;
339+
} else if cnt > 0 {
340+
return false
341+
}
342+
}
343+
344+
cnt * 2 == j - i + 1
345+
};
346+
347+
let mut ans = 0;
348+
let n = s.len();
349+
for i in 0..n - 1 {
350+
for j in (i + 1..n).rev() {
351+
if j - i + 1 < ans {
352+
break;
353+
}
354+
355+
if check(i, j) {
356+
ans = std::cmp::max(ans, j - i + 1);
357+
break;
358+
}
359+
}
360+
}
361+
362+
ans as i32
363+
}
364+
}
365+
```
366+
367+
```rust
368+
impl Solution {
369+
pub fn find_the_longest_balanced_substring(s: String) -> i32 {
370+
let mut zero = 0;
371+
let mut one = 0;
372+
let mut ans = 0;
373+
374+
for &c in s.as_bytes().iter() {
375+
if c == b'0' {
376+
if one > 0 {
377+
zero = 0;
378+
one = 0;
379+
}
380+
zero += 1;
381+
} else {
382+
one += 1;
383+
ans = std::cmp::max(ans, std::cmp::min(zero, one) * 2)
384+
}
385+
}
386+
387+
ans
388+
}
389+
}
390+
```
391+
328392
### **...**
329393

330394
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn find_the_longest_balanced_substring(s: String) -> i32 {
3+
let mut zero = 0;
4+
let mut one = 0;
5+
let mut ans = 0;
6+
7+
for &c in s.as_bytes().iter() {
8+
if c == b'0' {
9+
if one > 0 {
10+
zero = 0;
11+
one = 0;
12+
}
13+
zero += 1;
14+
} else {
15+
one += 1;
16+
ans = std::cmp::max(ans, std::cmp::min(zero, one) * 2)
17+
}
18+
}
19+
20+
ans
21+
}
22+
}

0 commit comments

Comments
 (0)