Facebook Interview Question
Software Engineer / Developersint MyStrStr(char * a,char * b)
{
int a_p = 0;
int b_p = 0;
int ret = -1;
if(a == NULL || b == NULL)
return -1;
while(true)
{
if(a[a_p] == '\0')
break;
if(a[a_p] == b[0])
{
ret = a_p;
b_p = 0;
//start check
while(true)
{
if(b[b_p] == '\0')
break;
if(a[a_p+b_p] != b[b_p])
{
return -1;
}
b_p++;
}
}
a_p++;
}
return ret;
}
The Knuth-Morris-Pratt or the Morris-Pratt algorithms are extensions of the basic Brute Force algorithm. They use precomputed data to skip forward not by 1 character, but by as many as possible for the search to succeed.
Here is some code
}
- randiv April 16, 2011