Skip to content

Commit 3c54c1d

Browse files
author
Mrinal Chauhan
committed
feat: added new algorithm Z Algorithm
1 parent e528b62 commit 3c54c1d

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

src/main/java/com/thealgorithms/strings/Zalgorithm.java

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,57 @@
44
import java.util.List;
55

66
/**
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.
98
*/
109
public final class Zalgorithm {
1110
private Zalgorithm() {
1211
}
1312

1413
/**
15-
* Calculates the Z array for a given string.
14+
* Finds the occurrences of a pattern in a text using Z-algorithm.
1615
*
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
2019
*/
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();
2525

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);
3629
}
3730
}
38-
return Z;
31+
return occurrences;
3932
}
4033

4134
/**
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.
4336
*
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
4740
*/
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;
5345

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;
5756
}
5857
}
59-
return occurrences;
58+
return z;
6059
}
6160
}

0 commit comments

Comments
 (0)