28 Implement strStr()
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
- The biggest part of the problem was identifing edge cases.
- O(N) time
- O(1) space
int strStr(string haystack, string needle) {
// are the same
if (needle == haystack) return 0;
// invalid cases
else if (haystack.length() == 0 ||
needle.length() > haystack.length()) return -1;
// nothing can be anything, so return first index
else if (needle.length() == 0) return 0;