please check the code for my last submission. it is passing only one test case.
Passing only one test case out of two
Your approach is flawed. At line 31, you assumed that you can get a string of atleast maximum consecutive same elements + k, but that won’t be true every time. For example, k = 2 string = abbb. Your code will print 5 while ans is 4.
The correct approach is to take a window with starting point at 0 and end streches until k b are found.That will be your current max substring of same elements with maximum k changes of b to a. Then slide the later end of b until there you find next b updating your ans. At that point your window will have k+1 b but only k changes are allowed so shift starting point of window until 1 b is thrown out of it. That is your new size of window. Do this process for whole string and you will get the ans for maximum length of substring with only a. Repeat the same for b. Ans will be max of these two.
@tanmayagrawal613 okay sir i will try coding this window approach. Also can you please share some tips about thinking the correct algorithm.
Don’t worry too much. Just practice daily increasing level of difficulty of problems and learn new topics.
With good enough practice you will be able to easily design an algorithm.
@tanmayagrawal613 thanks for your help i have coded the sliding window approach and submitted it pls check if it can be further optimised for time or space
If you got an AC, then the objective is completed. Move forward with other questions while keeping in mind about this problem. And after learning new techniques see if you can get a better approach.