Implement strStr().
Returns the index of the first occurrence of needle in haystack,
Returns the index of the first occurrence of needle in haystack,
or -1 if needle is not part of haystack.
class Solution {
public:
int strStr(string haystack, string needle) {
int len_h = haystack.size();
int len_n = needle.size();
if(len_n == 0) return 0;
if(len_h < len_n) return -1;
int n=0;
for (int i=0;i<len_h-len_n+1;i++)
{
if(haystack[i] == needle[n])
{
int j=0;
for(;j<len_n;j++)
{
if(haystack[i+j] != needle[j]) break;
}
if(j==len_n) return i;
}
}
return -1;
}
};