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