First remove all repeated consecutive substring with length 1.......
Software QA/Tests Interview Questions from Microsoft
(Continued from previous question...)
First remove all repeated consecutive substring with length 1.......
Q:
Microsoft Interview Question for Software Engineer in Tests about Algorithm
First remove all repeated consecutive substring with length 1, then delete substring of length 2 and so on...
Example : string is “abcabeccced”
After removing repeated substring of length 1: “abcababceccced” --> “abcababceced” (2 'c' are removed)
After removing repeated substring of length 2: “abcababceced” --> “abcabceced” (substring “ab” is removed)
and so on...
A:
void removeRepeat(char *s, int patternLength)
{
if(s && patternLength)
{
int strlength=strlen(s);
if(strlength>patternLength)
{
int tokenbegin=0;
int patterni=0;
int pos=0;
int newstringi=tokenbegin+patternLength;
int checki=tokenbegin + patternLength;
while(s[checki]!='\0')
{
if(s[checki+pos]==s[patterni+pos])
{
pos++;
if(pos==patternLength)
{
pos=0;
checki+=patternLength;
}
}
else
{
for(int i=pos;i>=0;--i)
s[newstringi++]=s[checki++];
patterni++;
pos=0;
}
if(checki>strlength)
break;
}
s[newstringi++]='\0';
}
}
}
int main(int argc, char *argv[])
{
char *c="abcababceccced";
char *cp=new char[1024];
strcpy(cp,c);
removeRepeat(cp,1);
printf("%s\n",cp);
removeRepeat(cp,2);
printf("%s\n",cp);
}
Output after 2 iterations:
abcababceced
abcabced
(Continued on next question...)
Other Interview Questions
|