Problem:
You are given a string. You have to eliminate the pairs (two same chars adjacent to each other).
eg. input string RGBBGBGR –> RGGBGR–> RBGR (output)
Solution:
We should check if we have character pair then cancel it. and then check for next character and previous character. Keep canceling the character until we either reach start of the array or end of the array or not find a pair.
Code
Thanks.
You are given a string. You have to eliminate the pairs (two same chars adjacent to each other).
eg. input string RGBBGBGR –> RGGBGR–> RBGR (output)
Solution:
We should check if we have character pair then cancel it. and then check for next character and previous character. Keep canceling the character until we either reach start of the array or end of the array or not find a pair.
Code
void removeAdjacentPair(char* str) { int len = strlen(str); int i,j=0; for(i=1; i < len; i++) { while((str[i]==str[j]) && (j >= 0)) { i++; j--; } str[++j] = str[i]; } str[j+1]='\0'; return; }
Thanks.
0 comments:
Post a Comment