C++ solution using KMP


#1

Comment body goes here.//kmp solution
int Solution::strStr(const string A, const string B) {
int l2=B.size();
int l1=A.size();
if(l1==0||l2==0)return -1;
int cur=0;
while(cur<l1-l2+1)
{
int index=0;
for(int i=cur;index<l2;i++)
{
if(A[i]==B[index])
index++;
else
{
if(A[i]==B[index-1]&&index>1)//deals with the case like bbba and bba
index=0;
break;
}

    }
    if(index==l2)
    return cur;
    else if (index==0) cur+=1;
    else 
    cur+=index; //for example foeyfox and fox then index 0 and 1 are useless so skip two position
}
return -1;

}