Sanket and strings

how to solve this problem ?

Hello @i_am_dekard_shaw

@S18ML0016 has provided quite a good explanation of this problem on the below thread.


Let me know if you still have any problem.

@prashuk156 https://ide.codingblocks.com/s/171032 whats wrong with my code?

You need to update the perfectness (maximum length substring of equal characters) in every scenario.

Here is the modified code

Let me know if you still have any problem.

@prashuk156 but according to algorithm you have to run a loop when t<k and inside this you have to see the occurrences of different char and increment t
while(t<k) {
if(a[i]!=‘a’){
t++;
}
i++;
}
as far as i have understood the algo…pls correct me if i am wrong

@i_am_dekard_shaw

You misunderstood something.
First let us understand the problem.
You are given a string “s” and number “k”. You can change atmost k characters from ‘a’ to ‘b’ or ‘b’ to ‘a’
After changing the characters you need to print the maximum continuous length which has all ‘a’ OR all ‘b’

Example
baabbabab
2

To get the maximum length I would change the string to “baabbbbbb” and the maximum length would then be 6

First you can try to implement a basic algo to solve this problem
The algo would be like

Getting maximum length of all ‘a’
Maintain a variable maxLengthA = 0
For every sub-string of “s”, count how many b’s are their
if number of b’s is less than k then we can change these to ‘a’
update maxLength = max(maxLength, length of this substring)

Similar is to be done for Getting maximum length of all ‘b’

Print max(maxLengthA, maxLengthB)

First try to implement this basic one then we will move on to the one at the link
// https://ide.codingblocks.com/s/171354

1 Like

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.