Post

4055 Longest Balanced Substring I

4055 Longest Balanced Substring I

Longest Balanced Substring I image

You are given a string s consisting of lowercase English letters.

A substring of s is called balanced if all distinct characters in the substring appear the same number of times.

Return the length of the longest balanced substring of s.

 

Example 1:

Input: s = “abbac”

Output: 4

Explanation:

The longest balanced substring is “abba” because both distinct characters ‘a’ and ‘b’ each appear exactly 2 times.

Example 2:

Input: s = “zzabccy”

Output: 4

Explanation:

The longest balanced substring is “zabc” because the distinct characters ‘z’, ‘a’, ‘b’, and ‘c’ each appear exactly 1 time.​​​​​​​

Example 3:

Input: s = “aba”

Output: 2

Explanation:

​​​​​​​One of the longest balanced substrings is “ab” because both distinct characters ‘a’ and ‘b’ each appear exactly 1 time. Another longest balanced substring is “ba”.

 

Constraints:

1
2
1 <= s.length <= 1000
s consists of lowercase English letters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

use std::collections::HashMap;
impl Solution {
    pub fn longest_balanced(s: String) -> i32 {
        // only 1000 as length
        //all substrings in 10^ 6
        let mut res = 0;
        for (i,k1) in s.chars().enumerate() {
            let mut hs: HashMap<char, i32> = HashMap::new();
            for (j,k2) in s[i..].chars().enumerate() {
               *hs.entry(k2).or_insert(0) += 1;
            
            let mut a = 0;
            let mut found = true;
            for &count in hs.values() {
                if a == 0 {
                    a = count;
                } else if a != count {
                    found = false;
                    break;
                }
            }
            if found {
                res = res.max(j+ 1 );
            }
        }
        }

        return res as i32;
    }
}



This post is licensed under CC BY 4.0 by the author.