Kmp
Algorithm
t = a b a b x a b a b x g
lps = 0 0 1 2 0 1 2 3 4 5 0Implementation
// OJ: https://leetcode.com/problems/implement-strstr/
// Author: github.com/lzl124631x
// Time: O(M+N)
// Space: O(N)
class Solution {
vector<int> getLps(string &s) {
int N = s.size(), i = 0;
vector<int> lps(N); // lps[0] is always 0
for (int j = 1; j < N; ++j) {
while (i > 0 && s[i] != s[j]) i = lps[i - 1]; // when s[i] can't match s[j], keep moving `i` backwards to `lps[i-1]`
i += s[i] == s[j]; // Move `i` if s[j] matches s[i]
lps[j] = i; // Assign the matched length to lps[j]
}
return lps;
}
public:
int strStr(string s, string t) {
if (t.empty()) return 0;
int M = s.size(), N = t.size(), i = 0, j = 0;
auto lps = getLps(t);
while (i < M) {
if (s[i] == t[j]) { // if match, move i and j once
++i;
++j;
if (j == N) return i - N; // Full match found.
} else {
if (j) j = lps[j - 1]; // The first lps[j-1] charaters are already matched. Start the next match from lps[j-1]
else ++i; // The first character is not matched, move i.
}
}
return -1;
}
};Problems
KMP Count
Problems
Reference
Last updated