KMP Algorithm
1. What is the time complexity of Naive-String-Searching Algorithm? (N = length of string in which pattern is searched, M = length of pattern to be searched)
2. What will be the output of the following code (cpp snippet)?
string s = "kmpalgo";
int n = s.size();
string s2 = "algo";
int m = s2.size();
int i=0;
int flag = 0;
while(i{
int j = i;
int k = 0;
int flag1 = 0;
while(j {
if(s[j] == s2[k])
{ j++; k++; }
else
{ flag1 = 1; break; }
}
if(flag1==0)
{ flag =1; break; }
i++;
}
cout << flag << endl;
3. What is the best-case time complexity of the naive string matching algorithm?
4. What is the worst-case scenario for the naive string matching algorithm?
5. Why is the naive algorithm called 'naive'?