-
Notifications
You must be signed in to change notification settings - Fork 0
28. Implement strStr()
PuChen0211 edited this page Sep 8, 2016
·
2 revisions
time is O(m * n)
haystack: qqqqqqqqqqqqq
needle: qqqa
int strStr(string haystack, string needle) {
int m = haystack.length();
int n = needle.length();
if (n > m) {
return -1;
}
for (int i = 0; i <= m - n; i++) {
int j = 0;
for (; j < n; j++) {
if (haystack[i + j] != needle[j]) {
break;
}
}
if (j == n) {
return i;
}
}
return -1;
}