https://leetcode.com/problems/implement-strstr/
- String
- Pattern Matching
Slide window through haystack and compare with needle.
O(n * m)
O(1)
class Solution {
public int strStr(String haystack, String needle) {
if (needle.length() == 0) return 0;
for (int i = 0; i <= haystack.length() - needle.length(); i++) {
if (haystack.substring(i, i + needle.length()).equals(needle)) return i;
}
return -1;
}
}