diff --git a/solution/3700-3799/3713.Longest Balanced Substring I/README.md b/solution/3700-3799/3713.Longest Balanced Substring I/README.md index 367b25f99c030..b027f31f2dd3b 100644 --- a/solution/3700-3799/3713.Longest Balanced Substring I/README.md +++ b/solution/3700-3799/3713.Longest Balanced Substring I/README.md @@ -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 + } +} +``` + diff --git a/solution/3700-3799/3713.Longest Balanced Substring I/README_EN.md b/solution/3700-3799/3713.Longest Balanced Substring I/README_EN.md index 147fd6d61fb04..3f3e216db6166 100644 --- a/solution/3700-3799/3713.Longest Balanced Substring I/README_EN.md +++ b/solution/3700-3799/3713.Longest Balanced Substring I/README_EN.md @@ -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 + } +} +``` + diff --git a/solution/3700-3799/3713.Longest Balanced Substring I/Solution.rs b/solution/3700-3799/3713.Longest Balanced Substring I/Solution.rs new file mode 100644 index 0000000000000..e8e0062a014d0 --- /dev/null +++ b/solution/3700-3799/3713.Longest Balanced Substring I/Solution.rs @@ -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 + } +}