Mam Im unable to understand that what question is asking to do.
Explain The Concept
Example:
1
abba
Output:
3
Explanation:
l=0: variable to mark the left index and initialized to 0
r=0: variable to mark the right index and initialized to 0
m=0: to store the perfectness and initialised to zero
t=k: to keep track of the number of replacements we can perform at any instance of time.
- First, we will check for the string of character ‘a’:
character at a[r] is ‘a’, increment r
r:1
the character at a[r] is not ‘a’ and t>0, increment r and decrement t as we can replace one ‘b’ to ‘a’
t:0 r:2
the character at a[r] is not ‘a’ and t==0. So, we cannot replace. Thus replace m by max. of m and r-l.
Repeat, while t==0:
A. if a[l]!=‘a’: increment t (because we must have done one replacement for this before and now as we are not considering this character so we can use this replacement somewhere else.)
B. increment l for each iteration of the loop.
m:2 t:0 l:1 t:1 l:2
the character at a[r] is not ‘a’ and t>0, increment r and decrement t as we can replace one ‘b’ to ‘a’
t:0 r:3
character at a[r] is ‘a’, increment r
r:4
replace m by max. of m and r-l.
m:2
l=0: variable to mark the left index and initialized to 0
r=0: variable to mark the right index and initialized to 0
m=0: to store the perfectness and initialised to zero
t=k: to keep track of the number of replacements we can perform at any instance of time.
- Now, we will check for the string of character ‘b’:
the character at a[r] is not ‘b’ and t>0, increment r and decrement t as we can replace one ‘a’ to ‘b’
t:0 r:1
character at a[r] is ‘b’, increment r
r:2
character at a[r] is ‘b’, increment r
r:3
the character at a[r] is not ‘b’ and t==0. So, we cannot replace. Thus replace m by max. of m and r-l.
Repeat, while t==0:
A. if a[l]!=‘b’: increment t (because we must have done one replacement for this before and now as we are not considering this character so we can use this replacement somewhere else.)
B. increment l for each iteration of the loop.
m:3 t:1 l:1
the character at a[r] is not ‘b’ and t>0, increment r and decrement t as we can replace one ‘a’ to ‘b’
t:0 r:4
replace m by max. of m and r-l.
m:3
Hence, the output is m:
3