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 only of the characters 'a', 'b', and 'c'.
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 = "aabcc"
Output: 3
Explanation:
The longest balanced substring is "abc" because all distinct characters '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 <= 105s contains only the characters 'a', 'b', and 'c'.Problem summary: You are given a string s consisting only of the characters 'a', 'b', and 'c'. 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"
"aabcc"
"aba"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3714: Longest Balanced Substring II
class Solution {
public int longestBalanced(String s) {
char[] cs = s.toCharArray();
int x = calc1(cs);
int y = Math.max(calc2(cs, 'a', 'b'), Math.max(calc2(cs, 'b', 'c'), calc2(cs, 'a', 'c')));
int z = calc3(cs);
return Math.max(x, Math.max(y, z));
}
private int calc1(char[] s) {
int res = 0;
int i = 0, n = s.length;
while (i < n) {
int j = i + 1;
while (j < n && s[j] == s[i]) {
j++;
}
res = Math.max(res, j - i);
i = j;
}
return res;
}
private int calc2(char[] s, char a, char b) {
int res = 0;
int i = 0, n = s.length;
while (i < n) {
while (i < n && s[i] != a && s[i] != b) {
i++;
}
Map<Integer, Integer> pos = new HashMap<>();
pos.put(0, i - 1);
int d = 0;
while (i < n && (s[i] == a || s[i] == b)) {
d += (s[i] == a) ? 1 : -1;
Integer prev = pos.get(d);
if (prev != null) {
res = Math.max(res, i - prev);
} else {
pos.put(d, i);
}
i++;
}
}
return res;
}
private int calc3(char[] s) {
Map<Long, Integer> pos = new HashMap<>();
pos.put(f(0, 0), -1);
int[] cnt = new int[3];
int res = 0;
for (int i = 0; i < s.length; i++) {
char c = s[i];
++cnt[c - 'a'];
int x = cnt[0] - cnt[1];
int y = cnt[1] - cnt[2];
long k = f(x, y);
Integer prev = pos.get(k);
if (prev != null) {
res = Math.max(res, i - prev);
} else {
pos.put(k, i);
}
}
return res;
}
private long f(int x, int y) {
return (x + 100000) << 20 | (y + 100000);
}
}
// Accepted solution for LeetCode #3714: Longest Balanced Substring II
func longestBalanced(s string) int {
x := calc1(s)
y := max(calc2(s, 'a', 'b'), calc2(s, 'b', 'c'), calc2(s, 'a', 'c'))
z := calc3(s)
return max(x, max(y, z))
}
func calc1(s string) int {
res := 0
n := len(s)
i := 0
for i < n {
j := i + 1
for j < n && s[j] == s[i] {
j++
}
if j-i > res {
res = j - i
}
i = j
}
return res
}
func calc2(s string, a, b byte) int {
res := 0
n := len(s)
i := 0
for i < n {
for i < n && s[i] != a && s[i] != b {
i++
}
pos := map[int]int{0: i - 1}
d := 0
for i < n && (s[i] == a || s[i] == b) {
if s[i] == a {
d++
} else {
d--
}
if prev, ok := pos[d]; ok {
if i-prev > res {
res = i - prev
}
} else {
pos[d] = i
}
i++
}
}
return res
}
type key struct {
x, y int
}
func calc3(s string) int {
pos := make(map[key]int)
pos[key{0, 0}] = -1
cnt := [3]int{}
res := 0
for i := 0; i < len(s); i++ {
c := s[i]
cnt[c-'a']++
x := cnt[0] - cnt[1]
y := cnt[1] - cnt[2]
k := key{x, y}
if j, ok := pos[k]; ok {
if i-j > res {
res = i - j
}
} else {
pos[k] = i
}
}
return res
}
# Accepted solution for LeetCode #3714: Longest Balanced Substring II
class Solution:
def longestBalanced(self, s: str) -> int:
def calc1(s: str) -> int:
res = 0
i, n = 0, len(s)
while i < n:
j = i + 1
while j < n and s[j] == s[i]:
j += 1
res = max(res, j - i)
i = j
return res
def calc2(s: str, a: str, b: str) -> int:
res = 0
i, n = 0, len(s)
while i < n:
while i < n and s[i] not in (a, b):
i += 1
pos = {0: i - 1}
d = 0
while i < n and s[i] in (a, b):
d += 1 if s[i] == a else -1
if d in pos:
res = max(res, i - pos[d])
else:
pos[d] = i
i += 1
return res
def calc3(s: str) -> int:
pos = {(0, 0): -1}
cnt = Counter()
res = 0
for i, c in enumerate(s):
cnt[c] += 1
k = (cnt["a"] - cnt["b"], cnt["b"] - cnt["c"])
if k in pos:
res = max(res, i - pos[k])
else:
pos[k] = i
return res
x = calc1(s)
y = max(calc2(s, "a", "b"), calc2(s, "b", "c"), calc2(s, "a", "c"))
z = calc3(s)
return max(x, y, z)
// Accepted solution for LeetCode #3714: Longest Balanced Substring II
use std::collections::HashMap;
impl Solution {
pub fn longest_balanced(s: String) -> i32 {
let x = Self::calc1(&s);
let y = Self::calc2(&s, 'a', 'b')
.max(Self::calc2(&s, 'b', 'c'))
.max(Self::calc2(&s, 'a', 'c'));
let z = Self::calc3(&s);
x.max(y).max(z)
}
fn calc1(s: &str) -> i32 {
let bytes = s.as_bytes();
let mut res = 0i32;
let mut i = 0usize;
let n = bytes.len();
while i < n {
let mut j = i + 1;
while j < n && bytes[j] == bytes[i] {
j += 1;
}
res = res.max((j - i) as i32);
i = j;
}
res
}
fn calc2(s: &str, a: char, b: char) -> i32 {
let bytes = s.as_bytes();
let a = a as u8;
let b = b as u8;
let mut res = 0i32;
let mut i = 0usize;
let n = bytes.len();
while i < n {
while i < n && bytes[i] != a && bytes[i] != b {
i += 1;
}
let mut pos: HashMap<i32, i32> = HashMap::new();
pos.insert(0, i as i32 - 1);
let mut d = 0i32;
while i < n && (bytes[i] == a || bytes[i] == b) {
if bytes[i] == a {
d += 1;
} else {
d -= 1;
}
if let Some(&pre) = pos.get(&d) {
res = res.max(i as i32 - pre);
} else {
pos.insert(d, i as i32);
}
i += 1;
}
}
res
}
fn f(x: i32, y: i32) -> i64 {
(((x + 100000) as i64) << 20) | ((y + 100000) as i64)
}
fn calc3(s: &str) -> i32 {
let mut pos: HashMap<i64, i32> = HashMap::new();
pos.insert(Self::f(0, 0), -1);
let mut cnt = [0i32; 3];
let mut res = 0i32;
for (i, c) in s.bytes().enumerate() {
cnt[(c - b'a') as usize] += 1;
let x = cnt[0] - cnt[1];
let y = cnt[1] - cnt[2];
let k = Self::f(x, y);
if let Some(&pre) = pos.get(&k) {
res = res.max(i as i32 - pre);
} else {
pos.insert(k, i as i32);
}
}
res
}
}
// Accepted solution for LeetCode #3714: Longest Balanced Substring II
function longestBalanced(s: string): number {
const x = calc1(s);
const y = Math.max(calc2(s, 'a', 'b'), calc2(s, 'b', 'c'), calc2(s, 'a', 'c'));
const z = calc3(s);
return Math.max(x, y, z);
}
function calc1(s: string): number {
let res = 0;
const n = s.length;
let i = 0;
while (i < n) {
let j = i + 1;
while (j < n && s[j] === s[i]) j++;
res = Math.max(res, j - i);
i = j;
}
return res;
}
function calc2(s: string, a: string, b: string): number {
let res = 0;
const n = s.length;
let i = 0;
while (i < n) {
while (i < n && s[i] !== a && s[i] !== b) i++;
const pos = new Map<number, number>();
pos.set(0, i - 1);
let d = 0;
while (i < n && (s[i] === a || s[i] === b)) {
d += s[i] === a ? 1 : -1;
const prev = pos.get(d);
if (prev !== undefined) {
res = Math.max(res, i - prev);
} else {
pos.set(d, i);
}
i++;
}
}
return res;
}
function calc3(s: string): number {
const pos = new Map<string, number>();
pos.set(key(0, 0), -1);
const cnt = [0, 0, 0];
let res = 0;
for (let i = 0; i < s.length; i++) {
const c = s.charCodeAt(i) - 97;
cnt[c]++;
const x = cnt[0] - cnt[1];
const y = cnt[1] - cnt[2];
const k = key(x, y);
const prev = pos.get(k);
if (prev !== undefined) {
res = Math.max(res, i - prev);
} else {
pos.set(k, i);
}
}
return res;
}
function key(x: number, y: number): string {
return x + '#' + y;
}
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.