File tree Expand file tree Collapse file tree 3 files changed +100
-0
lines changed
solution/3700-3799/3713.Longest Balanced Substring I Expand file tree Collapse file tree 3 files changed +100
-0
lines changed Original file line number Diff line number Diff line change @@ -215,6 +215,41 @@ function longestBalanced(s: string): number {
215215}
216216```
217217
218+ #### Rust
219+
220+ ``` rust
221+ impl Solution {
222+ pub fn longest_balanced (s : String ) -> i32 {
223+ let n : i32 = s . len () as i32 ;
224+ let bytes = s . as_bytes ();
225+ let mut ans : i32 = 0 ;
226+
227+ for i in 0 .. n {
228+ let mut cnt : [i32 ; 26 ] = [0 ; 26 ];
229+ let mut mx : i32 = 0 ;
230+ let mut v : i32 = 0 ;
231+
232+ for j in i .. n {
233+ let c : usize = (bytes [j as usize ] - b 'a' ) as usize ;
234+ cnt [c ] += 1 ;
235+
236+ if cnt [c ] == 1 {
237+ v += 1 ;
238+ }
239+
240+ mx = mx . max (cnt [c ]);
241+
242+ if mx * v == j - i + 1 {
243+ ans = ans . max (j - i + 1 );
244+ }
245+ }
246+ }
247+
248+ ans
249+ }
250+ }
251+ ```
252+
218253<!-- tabs: end -->
219254
220255<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -210,6 +210,41 @@ function longestBalanced(s: string): number {
210210}
211211```
212212
213+ #### Rust
214+
215+ ``` rust
216+ impl Solution {
217+ pub fn longest_balanced (s : String ) -> i32 {
218+ let n : i32 = s . len () as i32 ;
219+ let bytes = s . as_bytes ();
220+ let mut ans : i32 = 0 ;
221+
222+ for i in 0 .. n {
223+ let mut cnt : [i32 ; 26 ] = [0 ; 26 ];
224+ let mut mx : i32 = 0 ;
225+ let mut v : i32 = 0 ;
226+
227+ for j in i .. n {
228+ let c : usize = (bytes [j as usize ] - b 'a' ) as usize ;
229+ cnt [c ] += 1 ;
230+
231+ if cnt [c ] == 1 {
232+ v += 1 ;
233+ }
234+
235+ mx = mx . max (cnt [c ]);
236+
237+ if mx * v == j - i + 1 {
238+ ans = ans . max (j - i + 1 );
239+ }
240+ }
241+ }
242+
243+ ans
244+ }
245+ }
246+ ```
247+
213248<!-- tabs: end -->
214249
215250<!-- solution: end -->
Original file line number Diff line number Diff line change 1+ impl Solution {
2+ pub fn longest_balanced ( s : String ) -> i32 {
3+ let n: i32 = s. len ( ) as i32 ;
4+ let bytes = s. as_bytes ( ) ;
5+ let mut ans: i32 = 0 ;
6+
7+ for i in 0 ..n {
8+ let mut cnt: [ i32 ; 26 ] = [ 0 ; 26 ] ;
9+ let mut mx: i32 = 0 ;
10+ let mut v: i32 = 0 ;
11+
12+ for j in i..n {
13+ let c: usize = ( bytes[ j as usize ] - b'a' ) as usize ;
14+ cnt[ c] += 1 ;
15+
16+ if cnt[ c] == 1 {
17+ v += 1 ;
18+ }
19+
20+ mx = mx. max ( cnt[ c] ) ;
21+
22+ if mx * v == j - i + 1 {
23+ ans = ans. max ( j - i + 1 ) ;
24+ }
25+ }
26+ }
27+
28+ ans
29+ }
30+ }
You can’t perform that action at this time.
0 commit comments