code:https://ide.codingblocks.com/s/148893
what is wrong in my code?? plz help
Sanket and string
Hello @shashakchandel,
The logic you have applied would not work for all the test cases:
Example:
1
abba
Expected Output:
3 (either for aaa or bbb)
Your Output:
2 (for aa)
Explanation:
Your code is checking
…1. if temp[i]!=temp[i+1] i.e. the adjacent characters are not same
…2. and if k>0 i.e. you can make a replacement
then make temp[i+1]=temp[i]
How it should be:
after replacing first a with b… the perfectness would be 3
or after replacing last a with b would also give 3 as perfectness
Suggestion:
You have to check for all the strings that you form with the same elements and then select the one having the maximum length
You can look for the following Approach:
Perform following for both the characters a and b individually,
- Take two pointers l and r to mark the left and right index of the string under consideration.
- starting from l=0,r=0,max=0,count=0.
- repeat until r <n(length of the string)
3.1. increase the count whenever you find a different character(by different we mean if we are forming a string of a only, then b is different).
3.2. while count is greater than k,
3.2.1. decrement the count by one if the element at lth index is different.
3.2.1. increment l.
3.3. Compare max with count for maximum value.
3.4. increment r.
Hope, this would help.
Give a like if you are satisfied.