let me know in details how to solve it and what exactly demand this problem
Help me what will be our approach
the question states that u are given a string that only has ‘a’ and ‘b’ and u are given an integer which tells u the maximum swap limit ( the characters u can swap at once ) and tell the length of perfect string
one which has all same character
eg :
abbaaab
k = 2
in this case
case1 will be swapping at most 2 b’s and calculating max length so if u change the first 2 bs then the string will be aaaaaab
or last 2 b`s abaaaaa
in the cases max perfect length is either 5 or 6 {EDIT}
also
CASE2;
if u decide to reverse 2 a`s then abbaaab: will be
bbbbaab
maxPerfectness == 4
update any 2 the maxLen will be less than 5
so u need to take max of CASE 1 & case 2
How are you calculating of maxPerfefect length.
in case1 both-> aaaaaab or abaaaaa. On which basis you are saying maxPerfect length 5 (but in both string consecutive b are different 6 and 5
Similarly in case 2 -> bbbbaab or abaaaaa.
Consecutive b->4 and a->5.
Simply mughe samghao ki basis me aap maxPerfect len identyfy kr rhe oo
yes sorry it will return answer 6
kitne ekh window meh same characters occur kar rahe h
So, fir mai entire string me treverse karunga phle ‘a’ replace krunga (starting se ). And fr ‘a’ ko replace karunga (from reverse order se ).
And finally mai max find kr lunga consecutive ‘a’ ka (like in case eg 6).
Similarly, ese hi ‘b’ ki krunga.
Then finally a and b ka bicj ka max find kar lunga.
This algo will be fine. (time coplex ka basis me).
Agar nhi share me approach.
This question can be solved using many approach
I m discussing 2 Pointer Approach
just maintain two values
Lets try to make maximum contiguous b using k swaps
Starting i=0,j=0,swaps_available=k
let say
To maximize contigous b
if(swap_available>=0)
{
… if(character_at_j==b)
… {
…,…increase j;
…}
… else
…{
…,…swap_available–;
…,…increase j;
…}
}
else
{
…if(character_at_i==b)
…{
…,…i++;
…}
…else
…{
…,…swap_available++;
…,…i++;
…}
}
DO same for finding maximum a and print the maximum between these.
