SanketAndStrings Run Error

Hi, I have got run error in 2 test cases. Can you guys please look into my code?

Hello @Kinjal

You misunderstood the problem statement somewhere.
Here you can find the detailed explanation of the problem statement.

Let me know if you still need any help.

Sir, I see your code but why you increment β€˜i’ if we have to change β€˜b’ to β€˜a’. But you increment β€˜i’ for both cases inside while loop. Why? So, when I have no more β€˜k’, then we send it to the else part where while loop is there.
> else {

  		while (i <= j) {
  			if (a[i] == 'b')
  				i++;
  			else {
  				i++;
  				break;
  			}
  		}
  		m = max(m, j-i+1);
  	}

So this condition will execute, but inside that loop every time you increment β€˜i’, why? I dont get that part. Can you kindly tell me about that?


Sir, I did some changes in my approach so can you kindly look into my code?
I just want to know, how can I improve my logic if still my approach is so time consuming and weak. In most of the time my logic is failed and give TLE or run error and my lines of codes also going bigger. So, am I doing something wrong!!! Please let me know.

@Kinjal

Let’s take an example
aaabbbababb 3
Here when I am at index 7, I see that from the start I have got 4 b’s but I can only change at most 3 β€˜b’ to β€˜a’
So I search for a β€˜b’ from the start and remove it from the length that I am considering
Here first I will get an β€˜a’ at index 0
Then an β€˜a’ at index 1 i++
Then an β€˜a’ at index 2 i++
Then a β€˜b’ at index 3 i++
I do i++ and stop at 4 because from index 4 to 7 there are 3 β€˜b’ again only which I can change to β€˜a’.

You can call me bhaiya.

@Kinjal

When you write
str[x] = β€˜a’ // The index β€˜x’ doesn’t exist so it is wrong. Rather write str+=β€˜a’
Also define string1 and string2 internally so that they are reset to empty strings in each iteration

I have modified your code and marked the changes
Here is the modified code

Let me know if you still need any help.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.