leetcode/src/s0028_find_the_index_of_the_first_occurrence_in_a_string.cpp
2022-11-15 15:58:41 +08:00

19 lines
471 B
C++

#include "s0028_find_the_index_of_the_first_occurrence_in_a_string.hpp"
int Solution::strStr(string haystack, string needle) {
int haystackLen = haystack.length();
int needleLen = needle.length();
for (int i{0}; i < haystackLen; ++i) {
for (int j{0}, iTmp = i; j < needleLen; ++j) {
if (haystack[iTmp] != needle[j]) {
break;
} else if (j == needleLen - 1) {
return i;
} else {
++iTmp;
}
}
}
return -1;
}