Question bank › Sliding Window
Dsa Sliding Window Easy

Balanced Characters in Substring

Given a string s consisting of characters 'a', 'b', and 'c', find the length of the longest substring that contains an equal number of 'a's, 'b's, and 'c's. Your program should read a string from standard input and print the length of this longest substring with balanced character counts. **Input Format:** The input contains the string s (1 ≤ |s| ≤ 10^5). **Output Format:** An integer representing the length of the longest balanced substring. **Example:** Input: aabbcc Output: 6 The entire string contains an equal number of 'a's, 'b's, and 'c's which is 2 each, thus the output is 6.

Key concepts

sliding_windowstringhash_map

Practise this out loud — free

Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.

🎙 Practise this question now
Part of InterviewLab's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.