Discription:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Solution:
int strStr(string haystack, string needle) {
if (needle.empty())
return 0;
int m = haystack.size(), n = needle.size();
for (int i = 0; i <= m - n; i++){
int j;
for (j = 0; j < n; j++){
if (haystack[i + j] != needle[j])
break;
}
if (j == needle.size())
return i;
}
return -1;
}
GitHub-Leetcode:
https://github.com/wenwu313/LeetCode