Its a string problem.First remove all repeated consecutive substring with length 1,then delete substring of length 2 and so on…
for eg if we have a string like this -> abcababceccced
After removing substring of length 1 we will get abcababceced
After removing substring of length 2 we will get abcabced
After removing substring of length 3 we will get abced
This will be the final output
I have devised an algorithm but it has has a complexity of O(n3) and this is not desirable at all.My algorithm is as follows
char str[20]="abcababceccced";
int len=strlen(a);
for(i=1;i<=len/2;i++){
for(j=0;j<len;){
bool flag=chk(a,j,i);//this function will check whether the substring starting at a[j] and a[j+i] of length i are same or not.
if(flag){
//remove the second same substring.
}
else
j=j+i;
}
}
I will be very grateful if someone comes up with a less complex algo in C++ for this certain problem.
Indeed, linear time is possible for each substring length, since you only want consecutive identical substrings. Just keep a counter a identical characters, and update the string when you’ve found a substring. Since you want to remove substrings of all possible lengths, the overall complexity is quadratic.
The following C code should be working:
This should print successively: