Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions solution/3700-3799/3713.Longest Balanced Substring I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,41 @@ function longestBalanced(s: string): number {
}
```

#### Rust

```rust
impl Solution {
pub fn longest_balanced(s: String) -> i32 {
let n: i32 = s.len() as i32;
let bytes = s.as_bytes();
let mut ans: i32 = 0;

for i in 0..n {
let mut cnt: [i32; 26] = [0; 26];
let mut mx: i32 = 0;
let mut v: i32 = 0;

for j in i..n {
let c: usize = (bytes[j as usize] - b'a') as usize;
cnt[c] += 1;

if cnt[c] == 1 {
v += 1;
}

mx = mx.max(cnt[c]);

if mx * v == j - i + 1 {
ans = ans.max(j - i + 1);
}
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
35 changes: 35 additions & 0 deletions solution/3700-3799/3713.Longest Balanced Substring I/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,41 @@ function longestBalanced(s: string): number {
}
```

#### Rust

```rust
impl Solution {
pub fn longest_balanced(s: String) -> i32 {
let n: i32 = s.len() as i32;
let bytes = s.as_bytes();
let mut ans: i32 = 0;

for i in 0..n {
let mut cnt: [i32; 26] = [0; 26];
let mut mx: i32 = 0;
let mut v: i32 = 0;

for j in i..n {
let c: usize = (bytes[j as usize] - b'a') as usize;
cnt[c] += 1;

if cnt[c] == 1 {
v += 1;
}

mx = mx.max(cnt[c]);

if mx * v == j - i + 1 {
ans = ans.max(j - i + 1);
}
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
30 changes: 30 additions & 0 deletions solution/3700-3799/3713.Longest Balanced Substring I/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
impl Solution {
pub fn longest_balanced(s: String) -> i32 {
let n: i32 = s.len() as i32;
let bytes = s.as_bytes();
let mut ans: i32 = 0;

for i in 0..n {
let mut cnt: [i32; 26] = [0; 26];
let mut mx: i32 = 0;
let mut v: i32 = 0;

for j in i..n {
let c: usize = (bytes[j as usize] - b'a') as usize;
cnt[c] += 1;

if cnt[c] == 1 {
v += 1;
}

mx = mx.max(cnt[c]);

if mx * v == j - i + 1 {
ans = ans.max(j - i + 1);
}
}
}

ans
}
}
Loading