Dsa
Sliding Window
Medium
Max Length of Equal Substrings
You are given a string 's' consisting of only 'a' and 'b'. You can modify at most 'k' characters from 'a' to 'b' or from 'b' to 'a'. Find the length of the longest substring containing all 'a' or all 'b' after performing at most 'k' modifications.
Input Format:
- The first line contains the string 's' (1 ≤ |s| ≤ 10^5).
- The second line contains an integer 'k' (0 ≤ k ≤ |s|).
Output Format:
- Print a single integer representing the maximum length of the substring that can be achieved.
Example:
Input:
aabbaaa
1
Output:
5
(The longest substring is 'aaaaa' which can be formed by changing one 'b' to 'a'.)
Key concepts
sliding_windowstringeq_length
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