Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

KMP Algorithm for Pattern Searching

User image

Published by

sanya sanya

Published at: 3rd Aug, 2023
1.65 mins read

The Knuth-Morris-Pratt (KMP) algorithm is a pattern searching algorithm that efficiently finds all occurrences of a pattern within a text. It utilizes a preprocessed array called the "failure function" or "longest prefix suffix array" to skip unnecessary comparisons.

Algorithm:

1. Preprocess the pattern:

- Construct the failure function array.

- Initialize two pointers, i = 0 and j = 1.

- Iterate from position 1 to the end of the pattern:

- If the characters at positions i is equal to that at j, set the value at j in the lps array to i+1, increment i and j.

- If the characters at positions i and j are different:

- If i is not equal to 0, update i to the value at i-1 in the lps array and continue the comparison.

- If i is equal to 0, set the value at j in the lps array to 0, increment j.

2. Search for pattern matches in the text:

- Initialize two pointers, "i" and "j," with i = 0 and j = 0.

- Iterate from position 0 to the end of the text:

- If the characters at i and j are the same, increment both i and j.

- If the characters at i and j are different:

- If j is not equal to 0, update j to the value atj-1 in the lps array and continue the comparison.

- If j is equal to 0, increment i.

3. Process a match:

- If j is equal to the length of the pattern, a match is found at position i - j in the text.

The preprocessing step has a time complexity of O(m), and the pattern searching step has a time complexity of O(n), where n is the length of the text and m is the length of the pattern. Hence overall time complexity is of O(n + m).

Library

WEB DEVELOPMENT

FAANG QUESTIONS