VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximum-length-substring-having-all-same-characters-after-k-changes/

⇱ Longest substring having all same characters after k changes - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Longest substring having all same characters after k changes

Last Updated : 20 Apr, 2026

Given a string s of length n consisting only of uppercase English letters ('A'–'Z') and an integer k (0 ≤ k ≤ n), you can change at most k characters in the string. Find the length of the longest substring that can be made of all same character after performing at most k changes.

Input : k = 2, s = ABABA
Output : 5
We can get maximum length by replacing 2 B's with A's

Input : k = 4, s = HHHHHH
Output : 6
We get maximum length 6 without any replacement

[Naive Approach] Check All Substrings with Frequency Count - O(n^3) Time and O(1) Space

  • Consider all substrings by choosing start l and end r.
  • For each substring, find the most frequent character..
  • If (substring length − max frequency) ≤ k, update the maximum length.

Dry run for k = 1, s = "ABA"

  1. l=0, r=0 "A": consider substring, freq={A:1}, maxFreq = 1; no replacements needed, valid, res = 1
  2. l=0, r=1 "AB": consider substring, freq={A:1,B:1}, maxFreq = 1; 1 replacement needed (make all same), valid, res = 2
  3. l=0, r=2 "ABA": consider substring, freq={A:2,B:1}, maxFreq = 2; 1 replacement needed, valid, res = 3
  4. l=1, r=1 "B": single character, always valid, res=3
  5. l=1, r=2 "BA": freq={B:1,A:1}, maxFreq=1; 1 replacement needed, valid, res=3
  6. l=2, r=2 "A": single character, always valid, res=3

For every substring, we compute the most frequent character and check if remaining characters (to be replaced) are within k; the maximum valid length obtained is 3.


Output
5
6

[Better Approach] Window Sliding for Every Character - O(26 * n) Time O(1) Space

  • Check each character from 'A' to 'Z' and treat it as the target character for the substring.
  • Use a sliding window to find the longest substring that can be converted to that character with at most k changes.
  • If the number of different characters exceeds k, shrink the window from the left.
  • Track the maximum substring length among all characters and return it.

Output
5
6

[Expected Approach] Window Sliding with Hash Map - O(n) Time and O(1) Space

  • Maintain a sliding window (l, r) and store character frequencies using a hash map.
  • Track the most frequent character (maxFreq) in the window.
  • If (window size − maxFreq) > k, shrink the window; otherwise expand it and update the maximum length.

Dry run for k = 2, s = "ABABA"

  • r=0 'A': include character, freq={A:1}, maxFreq = 1; window valid since (size - maxFreq) = 0 ≤ k, so expand and update res = 1
  • r=1 'B': include character, freq={A:1,B:1}, maxFreq = 1; window valid (1 ≤ k), expand and update res = 2
  • r=2 'A': include character, freq={A:2,B:1}, maxFreq = 2; window valid (1 ≤ k), expand and update res = 3
  • r=3 'B': include character, freq={A:2,B:2}, maxFreq = 2; window valid (2 ≤ k), expand and update res = 4
  • r=4 'A': include character, freq={A:3,B:2}, maxFreq = 3; window valid (2 ≤ k), expand and update res = 5

Final answer is 5.


Output
5
6
Comment
Article Tags: