what is the problem in this code??
can you please comment out the errors and also the logical error if any?
Remove duplicates
Hey @mkidwai74 see for this test case:
aaaabbccccaaaabcbacba
Your output is performing like this:
Same element at ip 0 & 1
Op till now is aaa
Same element at ip 4 & 5
Op till now is baa
Same element at ip 6 & 7
Op till now is ccc
Same element at ip 10 & 11
Op till now is aaa
i is at a with j equal to 0
aaaaaa
This is because every time your value of j is qual to 0 in each recursive call
Take reference from this

what is happening inside the while loop??
can you dry run that part?
See this for input, aaabbbcdddd
a of 0 is equal to 1,
/*This is inside while loop
So a of 1 will have value for a 2
So a of 2 will have value for a 3
,So a of 3 will have value for a 4
,So a of 4 will have value for a 5
,So a of 5 will have value for a 6
,So a of 6 will have value for a 7
,So a of 7 will have value for a 8
,So a of 8 will have value for a 9
,So a of 9 will have value for a 10
,So a of 10 will have value for a 11
*/
Resulting string is aabbbcdddd
Calling reccursive call for 0
a of 0 is equal to 1,
/*This is inside while loop
So a of 1 will have value for a 2
,So a of 2 will have value for a 3
,So a of 3 will have value for a 4
,So a of 4 will have value for a 5
,So a of 5 will have value for a 6
,So a of 6 will have value for a 7
,So a of 7 will have value for a 8
,So a of 8 will have value for a 9
,So a of 9 will have value for a 10
*/
Resulting string is abbbcdddd
Calling reccursive call for 0
a of 1 is equal to 2,
/*This is inside while loop
So a of 2 will have value for a 3
,So a of 3 will have value for a 4
,So a of 4 will have value for a 5
,So a of 5 will have value for a 6
,So a of 6 will have value for a 7
,So a of 7 will have value for a 8
,So a of 8 will have value for a 9
*/
Resulting string is abbcdddd
Calling reccursive call for 1
a of 1 is equal to 2,
/*This is inside while loop
So a of 2 will have value for a 3
,So a of 3 will have value for a 4
,So a of 4 will have value for a 5
,So a of 5 will have value for a 6
,So a of 6 will have value for a 7
,So a of 7 will have value for a 8
*/
Resulting string is abcdddd
Calling reccursive call for 1
a of 3 is equal to 4,
/*This is inside while loop
So a of 4 will have value for a 5
,So a of 5 will have value for a 6
,So a of 6 will have value for a 7
*/
Resulting string is abcddd
Calling reccursive call for 3
a of 3 is equal to 4,
/*This is inside while loop
So a of 4 will have value for a 5
,So a of 5 will have value for a 6
*/
Resulting string is abcdd
Calling reccursive call for 3
a of 3 is equal to 4,
/*This is inside while loop
So a of 4 will have value for a 5
*/
Resulting string is abcd
Calling reccursive call for 3
final string ->abcd
You can check this for different input from here
Have added cout statement for you for better understanding.
Thanks alot i understood it now
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.