|
3 | 3 | import java.util.ArrayList; |
4 | 4 | import java.util.List; |
5 | 5 |
|
6 | | - |
| 6 | +/** |
| 7 | + * Implementation of the Knuth–Morris–Pratt (KMP) string matching algorithm. |
| 8 | + * KMP efficiently searches for occurrences of a pattern within a text by |
| 9 | + * utilizing a pre-computed failure function to avoid redundant comparisons. |
| 10 | + * Time Complexity: O(n + m) where n is text length and m is pattern length. |
| 11 | + */ |
7 | 12 | final class KnuthMorrisPratt { |
8 | | - private KnuthMorrisPratt() { |
9 | | - } |
| 13 | + private KnuthMorrisPratt() {} |
10 | 14 |
|
11 | | - // Compute the longest proper prefix which is also suffix (LPS) array |
12 | | - public static int[] computeLps(final String pattern) { |
13 | | - final int n = pattern.length(); |
14 | | - final int[] lps = new int[n]; |
15 | | - int len = 0; // length of the previous longest prefix suffix |
16 | | - lps[0] = 0; |
17 | | - for (int i = 1; i < n; ) { |
18 | | - if (pattern.charAt(i) == pattern.charAt(len)) { |
19 | | - len++; |
20 | | - lps[i] = len; |
21 | | - i++; |
22 | | - } else { |
23 | | - if (len != 0) { |
24 | | - len = lps[len - 1]; |
25 | | - } else { |
26 | | - lps[i] = 0; |
27 | | - i++; |
28 | | - } |
29 | | - } |
30 | | - } |
31 | | - return lps; |
32 | | - } |
| 15 | + /** |
| 16 | + * Compute the Longest Proper Prefix which is also Suffix (LPS) array |
| 17 | + * for the given pattern. This array is used to avoid unnecessary |
| 18 | + * character comparisons during the search phase. |
| 19 | + * |
| 20 | + * @param pattern the pattern to compute LPS for |
| 21 | + * @return the LPS array |
| 22 | + */ |
| 23 | + public static int[] computeLps(final String pattern) { |
| 24 | + final int n = pattern.length(); |
| 25 | + final int[] lps = new int[n]; |
| 26 | + int len = 0; |
| 27 | + lps[0] = 0; |
| 28 | + for (int i = 1; i < n; ) { |
| 29 | + if (pattern.charAt(i) == pattern.charAt(len)) { |
| 30 | + len++; |
| 31 | + lps[i] = len; |
| 32 | + i++; |
| 33 | + } else { |
| 34 | + if (len != 0) { |
| 35 | + len = lps[len - 1]; |
| 36 | + } else { |
| 37 | + lps[i] = 0; |
| 38 | + i++; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + return lps; |
| 43 | + } |
33 | 44 |
|
34 | | - // Return list of start indices where pattern occurs in text |
35 | | - public static List<Integer> search(final String text, final String pattern) { |
36 | | - final List<Integer> occurrences = new ArrayList<>(); |
37 | | - if (pattern == null || pattern.isEmpty() || text == null) { |
38 | | - return occurrences; |
39 | | - } |
| 45 | + /** |
| 46 | + * Search for all occurrences of the pattern in the text. |
| 47 | + * Returns a list of starting indices where the pattern is found. |
| 48 | + * |
| 49 | + * @param text the text to search in |
| 50 | + * @param pattern the pattern to search for |
| 51 | + * @return list of starting indices of pattern occurrences |
| 52 | + */ |
| 53 | + public static List<Integer> search(final String text, final String pattern) { |
| 54 | + final List<Integer> occurrences = new ArrayList<>(); |
| 55 | + if (pattern == null || pattern.isEmpty() || text == null) { |
| 56 | + return occurrences; |
| 57 | + } |
40 | 58 |
|
41 | | - final int[] lps = computeLps(pattern); |
42 | | - int i = 0; // index for text |
43 | | - int j = 0; // index for pattern |
44 | | - final int n = text.length(); |
45 | | - final int m = pattern.length(); |
46 | | - while (i < n) { |
47 | | - if (text.charAt(i) == pattern.charAt(j)) { |
48 | | - i++; |
49 | | - j++; |
50 | | - if (j == m) { |
51 | | - occurrences.add(i - j); |
52 | | - j = lps[j - 1]; |
53 | | - } |
54 | | - } else { |
55 | | - if (j != 0) { |
56 | | - j = lps[j - 1]; |
57 | | - } else { |
58 | | - i++; |
59 | | - } |
60 | | - } |
61 | | - } |
62 | | - return occurrences; |
63 | | - } |
| 59 | + final int[] lps = computeLps(pattern); |
| 60 | + int i = 0; |
| 61 | + int j = 0; |
| 62 | + final int n = text.length(); |
| 63 | + final int m = pattern.length(); |
| 64 | + while (i < n) { |
| 65 | + if (text.charAt(i) == pattern.charAt(j)) { |
| 66 | + i++; |
| 67 | + j++; |
| 68 | + if (j == m) { |
| 69 | + occurrences.add(i - j); |
| 70 | + j = lps[j - 1]; |
| 71 | + } |
| 72 | + } else { |
| 73 | + if (j != 0) { |
| 74 | + j = lps[j - 1]; |
| 75 | + } else { |
| 76 | + i++; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + return occurrences; |
| 81 | + } |
64 | 82 |
|
65 | | - // example runner |
66 | | - public static void main(String[] args) { |
67 | | - final String text = "AAAAABAAABA"; |
68 | | - final String pattern = "AAAA"; |
69 | | - final List<Integer> idx = search(text, pattern); |
70 | | - for (int pos : idx) { |
71 | | - System.out.println("Pattern starts: " + pos); |
72 | | - } |
73 | | - } |
| 83 | + /** |
| 84 | + * Main method demonstrating the KMP algorithm with an example. |
| 85 | + * |
| 86 | + * @param args command line arguments (unused) |
| 87 | + */ |
| 88 | + public static void main(String[] args) { |
| 89 | + final String text = "AAAAABAAABA"; |
| 90 | + final String pattern = "AAAA"; |
| 91 | + final List<Integer> idx = search(text, pattern); |
| 92 | + for (int pos : idx) { |
| 93 | + System.out.println("Pattern found at index: " + pos); |
| 94 | + } |
| 95 | + } |
74 | 96 | } |
0 commit comments