|
4 | 4 | import java.util.List;
|
5 | 5 |
|
6 | 6 | /**
|
7 |
| - * Class to implement the Z Algorithm for pattern matching in strings. |
8 |
| - * The Z Algorithm finds all occurrences of a pattern within a text. |
| 7 | + * Z-algorithm implementation to find all occurrences of a pattern in a text. |
9 | 8 | */
|
10 | 9 | public final class Zalgorithm {
|
11 | 10 | private Zalgorithm() {
|
12 | 11 | }
|
13 | 12 |
|
14 | 13 | /**
|
15 |
| - * Calculates the Z array for a given string. |
| 14 | + * Finds the occurrences of a pattern in a text using Z-algorithm. |
16 | 15 | *
|
17 |
| - * @param s the input string |
18 |
| - * @return the Z array where Z[i] indicates the length of the longest substring |
19 |
| - * starting from s[i] that is also a prefix of s |
| 16 | + * @param text the input text in which we are searching the pattern |
| 17 | + * @param pattern the pattern to search for |
| 18 | + * @return a list of indices where the pattern occurs in the text |
20 | 19 | */
|
21 |
| - public static int[] calculateZ(String s) { |
22 |
| - int n = s.length(); |
23 |
| - int[] Z = new int[n]; |
24 |
| - int left = 0, right = 0; |
| 20 | + public static List<Integer> findPatternOccurrences(String text, String pattern) { |
| 21 | + String combined = pattern + "$" + text; |
| 22 | + int[] zArray = calculateZ(combined); |
| 23 | + List<Integer> occurrences = new ArrayList<>(); |
| 24 | + int patternLength = pattern.length(); |
25 | 25 |
|
26 |
| - for (int i = 1; i < n; i++) { |
27 |
| - if (i <= right) { |
28 |
| - Z[i] = Math.min(right - i + 1, Z[i - left]); |
29 |
| - } |
30 |
| - while (i + Z[i] < n && s.charAt(Z[i]) == s.charAt(i + Z[i])) { |
31 |
| - Z[i]++; |
32 |
| - } |
33 |
| - if (i + Z[i] - 1 > right) { |
34 |
| - left = i; |
35 |
| - right = i + Z[i] - 1; |
| 26 | + for (int i = 0; i < zArray.length; i++) { |
| 27 | + if (zArray[i] == patternLength) { |
| 28 | + occurrences.add(i - patternLength - 1); |
36 | 29 | }
|
37 | 30 | }
|
38 |
| - return Z; |
| 31 | + return occurrences; |
39 | 32 | }
|
40 | 33 |
|
41 | 34 | /**
|
42 |
| - * Finds all occurrences of a pattern in the text using the Z Algorithm. |
| 35 | + * Helper method to calculate Z-array for a given string. |
43 | 36 | *
|
44 |
| - * @param text the text in which to search for the pattern |
45 |
| - * @param pattern the pattern to search for |
46 |
| - * @return a list of starting indices of occurrences of the pattern in the text |
| 37 | + * @param s the input string |
| 38 | + * @return the Z-array where Z[i] is the length of the longest substring |
| 39 | + * starting from i that is also a prefix of s |
47 | 40 | */
|
48 |
| - public static List<Integer> findPatternOccurrences(String text, String pattern) { |
49 |
| - String combined = pattern + "$" + text; |
50 |
| - int[] Z = calculateZ(combined); |
51 |
| - List<Integer> occurrences = new ArrayList<>(); |
52 |
| - int patternLength = pattern.length(); |
| 41 | + private static int[] calculateZ(String s) { |
| 42 | + int n = s.length(); |
| 43 | + int[] z = new int[n]; |
| 44 | + int l = 0, r = 0; |
53 | 45 |
|
54 |
| - for (int i = 0; i < Z.length; i++) { |
55 |
| - if (Z[i] == patternLength) { |
56 |
| - occurrences.add(i - patternLength - 1); |
| 46 | + for (int i = 1; i < n; i++) { |
| 47 | + if (i <= r) { |
| 48 | + z[i] = Math.min(r - i + 1, z[i - l]); |
| 49 | + } |
| 50 | + while (i + z[i] < n && s.charAt(z[i]) == s.charAt(i + z[i])) { |
| 51 | + z[i]++; |
| 52 | + } |
| 53 | + if (i + z[i] - 1 > r) { |
| 54 | + l = i; |
| 55 | + r = i + z[i] - 1; |
57 | 56 | }
|
58 | 57 | }
|
59 |
| - return occurrences; |
| 58 | + return z; |
60 | 59 | }
|
61 | 60 | }
|
0 commit comments