Skip to content

Commit

Permalink
Update KMP algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
jeantimex committed Oct 20, 2018
1 parent 6650633 commit b0adfa3
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/string/implement-strstr.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,17 @@ const strStrKMP = (haystack, needle) => {
*/
const getLPS = s => {
const lps = Array(s.length).fill(0);

let i = 1;
let len = 0;

while (i < s.length) {
if (s[i] === s[len]) {
lps[i++] = ++len;
} else if (len === 0) {
lps[i++] = 0;
} else {
} else if (len > 0) {
len = lps[len - 1];
} else {
i++;
}
}

Expand Down

0 comments on commit b0adfa3

Please sign in to comment.