File tree 3 files changed +150
-0
lines changed
solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String
3 files changed +150
-0
lines changed Original file line number Diff line number Diff line change @@ -335,6 +335,70 @@ function findTheLongestBalancedSubstring(s: string): number {
335
335
}
336
336
```
337
337
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
+
338
402
### ** ...**
339
403
340
404
```
Original file line number Diff line number Diff line change @@ -325,6 +325,70 @@ function findTheLongestBalancedSubstring(s: string): number {
325
325
}
326
326
```
327
327
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
+
328
392
### ** ...**
329
393
330
394
```
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments