LeetCode 424 Longest Repeating Character Replacement - Why Naive Fails

The trap

Try all substrings. For each, count character frequencies and check if you can make all characters the same with at most k replacements.

There are O(n2)O(n^2) substrings. Counting frequencies for each takes O(n)O(n). Total: O(n3)O(n^3).

For n=105n = 10^5, that's 101510^{15} operations. Far too slow.