JavaScript Algorithms and Data Structures: Strings - Knuth–Morris–Pratt Algorithm

Knuth–Morris–Pratt algorithm (or KMP algorithm) is a string-searching algorithm that finds all occurrences of a pattern string in a text string. Learn how to implement KMP algorithm in JavaScript.

The Knuth–Morris–Pratt string searching algorithm (or KMP algorithm) searches for occurrences of a "word" W within a main "text string" T by employing the observation that when a mismatch occurs, the word itself embodies sufficient information to determine where the next match could begin, thus bypassing re-examination of previously matched characters.

Complexity

  • Time: O(|W| + |T|) (much faster comparing to trivial O(|W| * |T|))
  • Space: O(|W|)

References

The Original Article can be found on https://github.com

#javascript #algorithms #datastructures #strings

JavaScript Algorithms and Data Structures: Strings - Knuth–Morris–Pratt Algorithm
3.85 GEEK