Interview Question
Software Engineer in TestsCountry: United States
Interview Type: Phone Interview
first of all, you didnt cover wildchar which can match multiple chars. Then what about this
input= aaaabc
pattern = aaabc
Let pattern(array, p[]) be of length M and given string(array, str[]) be of length N. Now,
int findMatchingIndex(char*str, char*p)
{
int i=0, j=0;
for(i=0;i<=N-M+1;i++)
{
if(str[i]==p[i])
{
int xored=1;
for(int j=0;j<M;j++)
{
xored *= str[i+j] ^ p[j];
if(xored==0)
return i; //index of first match
}
}
}
return -1; //not found
}
Use pointer to pointing to the start of two string. Compare if match. If yes, move the pointer to PATTERN together with pointer to INPUT. If match not found, reset pointer to PATTERN to the begin.
- peter tang October 31, 2012