Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Move from brute-force thinking to an efficient approach using hash map strategy.
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 <= s.length <= 1000s consists of lowercase English letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map
"abbac"
"zzabccy"
"aba"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3713: Longest Balanced Substring I
class Solution {
public int longestBalanced(String s) {
int n = s.length();
int[] cnt = new int[26];
int ans = 0;
for (int i = 0; i < n; ++i) {
Arrays.fill(cnt, 0);
int mx = 0, v = 0;
for (int j = i; j < n; ++j) {
int c = s.charAt(j) - 'a';
if (++cnt[c] == 1) {
++v;
}
mx = Math.max(mx, cnt[c]);
if (mx * v == j - i + 1) {
ans = Math.max(ans, j - i + 1);
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #3713: Longest Balanced Substring I
func longestBalanced(s string) (ans int) {
n := len(s)
for i := 0; i < n; i++ {
cnt := [26]int{}
mx, v := 0, 0
for j := i; j < n; j++ {
c := s[j] - 'a'
cnt[c]++
if cnt[c] == 1 {
v++
}
mx = max(mx, cnt[c])
if mx*v == j-i+1 {
ans = max(ans, j-i+1)
}
}
}
return ans
}
# Accepted solution for LeetCode #3713: Longest Balanced Substring I
class Solution:
def longestBalanced(self, s: str) -> int:
n = len(s)
ans = 0
for i in range(n):
cnt = Counter()
mx = v = 0
for j in range(i, n):
cnt[s[j]] += 1
mx = max(mx, cnt[s[j]])
if cnt[s[j]] == 1:
v += 1
if mx * v == j - i + 1:
ans = max(ans, j - i + 1)
return ans
// Accepted solution for LeetCode #3713: Longest Balanced Substring I
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
}
}
// Accepted solution for LeetCode #3713: Longest Balanced Substring I
function longestBalanced(s: string): number {
const n = s.length;
let ans: number = 0;
for (let i = 0; i < n; ++i) {
const cnt: number[] = Array(26).fill(0);
let [mx, v] = [0, 0];
for (let j = i; j < n; ++j) {
const c = s[j].charCodeAt(0) - 97;
if (++cnt[c] === 1) {
++v;
}
mx = Math.max(mx, cnt[c]);
if (mx * v === j - i + 1) {
ans = Math.max(ans, j - i + 1);
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan the rest of the array looking for a match. Two nested loops give n × (n−1)/2 comparisons = O(n²). No extra space since we only use loop indices.
One pass through the input, performing O(1) hash map lookups and insertions at each step. The hash map may store up to n entries in the worst case. This is the classic space-for-time tradeoff: O(n) extra memory eliminates an inner loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.