Even looking at the solution I had to think twice. This feeling sucks, man! Given a string with lowercase letters only, if you are allowed to replace no more than ‘k’ letters with any letter, find the length of the longest substring having the same letters after replacement. Example 1: aabccbb Input: String="aabccbb", k=2 Output: 5 Explanation: Replace the two 'c' with 'b' to have a longest repeating substring "bbbbb". Example 2: Input: String="abbcb", k=1 Output: 4 Explanation: Replace the 'c' with 'b' to have a longest repeating substring "bbbb". Example 3: Input: String="abccde", k=1 Output: 3 Explanation: Replace the 'b' or 'd' with 'c' to have the longest repeating substring "ccc".
Experiment.
I can give you a hint, start from both the end and try to keep your dictionary count of letters. You can also use sliding window here.
You're not only the one. Don't loose hope. The more you practice you'll understand the patterns of questions and you'll see how you're picking up. I had the same situation two weeks ago. Now I read the question twice without going to explanation and evaluate my understanding with the explanation later. Same with the solution..try solving on your own with brute force approach. Then go find other solutions and compare your idealogy. This is how I self taught myself.
what if you only wanted the length of the longest substring of just "a"s after replacing k non-"a"s?
Don’t lose hope. Stay positive. Take this as a learning exercise and not like a fight (you against the problem). Your goal should be learning how to solve problems, not to solve them right away! Of course, at some point, when the concepts have engrained in your brain you will be able to start solving problems by your own, but even then you still want to check the solutions to improve your implementation even more.
2 pointers
Don't get discouraged. This is not a simplest of the problem. And there can be several different solutions, simpler ones will be less efficient, and as you try to make it more efficient , solution gets complex.
Take your time.