three test case fail
String Remove Duplicate
updated code added comments where updates are made
u did not consider the condition where more than 2 same character occur consequently
eg aaaaadfsfssss
@chhavibansal what is the use to j variable here and also can you explain with recurrence for aaaab
how it is working ?
hi @Vikaspal sorry for the redundent j pointer its of no use i was trying to do it in another way ealier & later forgot to update it
u can remove j from all paramters
aaaab
first of all i =0
u`ll print the first character always
thne while( i +1 < n and a[i] == a[i+1]) i++;
i will increment upto last ‘a’ position then
duplicateString(str,i+1, j , n);
so now we print b
then just
duplicateString(str,i+1, j , n);
so here we hit the base condition and return to int main
@chhavibansal can we also do that because from by approach i am trying to shift the left today the left side is they are adjacent and same hope got my point? And please tell me the correct way or doing this so i can improve further?
so use the j pointer like i passed in the paramter
just
where I print the character u put that character at jth position increment j
in int main() just print the string till j the position
the code would be something like
fn( char* ch , int i , int &j , int n) {
// base condition
ch[j++] = ch[i];
while( i+1 < n and ch[i] == ch[i+1]) i++;
fn(ch , i +1 , j , n) ;
}
int main(){
int j = 0 ; // pass j by refernce
char ch[1000];
cin >> ch;
fn( ch , 0 , j , sizeofCh ) ;
for(int i = 0 ; i < j ; j++)
cout << ch[i];
// just print uptil j it has not repeeated character
}
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.