Explorer
Data Structures & Algorithms

Compute the Longest Prefix Suffix (LPS) array for KMP algorithm.

Problem Statement

Given a string `pattern`, calculate the Longest Prefix Suffix (LPS) array used in the Knuth-Morris-Pratt (KMP) algorithm. Each entry `LPS[i]` stores the length of the longest proper prefix of `pattern[0...i]` that is also a suffix of `pattern[0...i]`.

Examples

Input: pattern = "abcab"

Output: [0, 0, 0, 1, 2]

Explanation: Combining the input according to KMP Algorithm or LPS array logic yields [0, 0, 0, 1, 2].

Input: pattern = "aaaa"

Output: [0, 1, 2, 3]

Explanation: Combining the input according to KMP Algorithm or LPS array logic yields [0, 1, 2, 3].

Complexity

Time Complexity: O(N)

Space Complexity: O(N)

Hints

šŸ’” Hint 1: LPS[i] stores the length of the longest proper prefix of pattern[0...i] that is also a suffix of pattern[0...i]. šŸ’” Hint 2: Use two pointers: len (length of previous longest prefix suffix) and i (current index starting at 1). šŸ’” Hint 3: If pattern[i] === pattern[len], increment len and store in lps[i]. Otherwise, fall back: len = lps[len - 1].

Editorial & Approach

Problem Overview & Intuition

The Longest Prefix Suffix (LPS) array allows KMP to avoid re-examining characters upon mismatch. By falling back to len = lps[len - 1] rather than resetting to 0, construction runs in linear O(N) time.

Step-by-Step Approach

  1. Initialize lps = new Array(n).fill(0), len = 0, and i = 1.
  2. While i < n:
  3. If pattern[i] === pattern[len]: len++, lps[i] = len, i++.
  4. Else if len !== 0: fall back len = lps[len - 1] without incrementing i.
  5. Else: lps[i] = 0, i++.
  6. Return lps.

Optimal Implementation (JavaScript)

function computeLPSArray(pattern) {
  const n = pattern.length;
  const lps = new Array(n).fill(0);
  let len = 0, i = 1;
  while (i < n) {
    if (pattern[i] === pattern[len]) {
      len++;
      lps[i] = len;
      i++;
    } else {
      if (len !== 0) {
        len = lps[len - 1];
      } else {
        lps[i] = 0;
        i++;
      }
    }
  }
  return lps;
}

Complexity Analysis

Time Complexity O(N) — len can decrease at most N times.
Space Complexity O(N) — array of size N.

Edge Cases & Corner Traps Handled

  • All identical characters "aaaa": lps = [0, 1, 2, 3].
  • No repeating prefixes: all zeroes.

KMP Algorithm or LPS array

Hard
Given a string `pattern`, calculate the Longest Prefix Suffix (LPS) array used in the Knuth-Morris-Pratt (KMP) algorithm. Each entry `LPS[i]` stores the length of the longest proper prefix of `pattern[0...i]` that is also a suffix of `pattern[0...i]`.
Example Scenarios
1Example 1
Input: pattern = "abcab"
Output: [0, 0, 0, 1, 2]
Explanation:

Combining the input according to KMP Algorithm or LPS array logic yields [0, 0, 0, 1, 2].

2Example 2
Input: pattern = "aaaa"
Output: [0, 1, 2, 3]
Explanation:

Combining the input according to KMP Algorithm or LPS array logic yields [0, 1, 2, 3].

Editor
Loading Editor...
pattern =
"abcab"
Output:Click "Run" above to execute and verify your code here.
[0,0,0,1,2]