Skip to content

Commit bccaf97

Browse files
Add Z-Algorithm for Linear-Time String Pattern Matching (#7124)
* Add Z-Algorithm (string pattern matching) with tests * Add Z-Algorithm (string pattern matching) with tests * Add Z-Algorithm (string pattern matching) with tests * Fix checkstyle errors for ZAlgorithm * Fix: clang-format and checkstyle compliance for ZAlgorithm
1 parent 746457c commit bccaf97

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@
822822
- 📄 [Upper](src/main/java/com/thealgorithms/strings/Upper.java)
823823
- 📄 [ValidParentheses](src/main/java/com/thealgorithms/strings/ValidParentheses.java)
824824
- 📄 [WordLadder](src/main/java/com/thealgorithms/strings/WordLadder.java)
825+
- 📄 [ZAlgorithm](src/main/java/com/thealgorithms/strings/ZAlgorithm.java)
825826
- 📁 **zigZagPattern**
826827
- 📄 [ZigZagPattern](src/main/java/com/thealgorithms/strings/zigZagPattern/ZigZagPattern.java)
827828
- 📁 **tree**
@@ -1579,6 +1580,7 @@
15791580
- 📄 [UpperTest](src/test/java/com/thealgorithms/strings/UpperTest.java)
15801581
- 📄 [ValidParenthesesTest](src/test/java/com/thealgorithms/strings/ValidParenthesesTest.java)
15811582
- 📄 [WordLadderTest](src/test/java/com/thealgorithms/strings/WordLadderTest.java)
1583+
- 📄 [ZAlgorithmTest](src/test/java/com/thealgorithms/strings/ZAlgorithmTest.java)
15821584
- 📁 **zigZagPattern**
15831585
- 📄 [ZigZagPatternTest](src/test/java/com/thealgorithms/strings/zigZagPattern/ZigZagPatternTest.java)
15841586
- 📁 **tree**
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* https://en.wikipedia.org/wiki/Z-algorithm
3+
*/
4+
package com.thealgorithms.strings;
5+
6+
public final class ZAlgorithm {
7+
8+
private ZAlgorithm() {
9+
throw new UnsupportedOperationException("Utility class");
10+
}
11+
12+
public static int[] zFunction(String s) {
13+
int n = s.length();
14+
int[] z = new int[n];
15+
int l = 0;
16+
int r = 0;
17+
18+
for (int i = 1; i < n; i++) {
19+
if (i <= r) {
20+
z[i] = Math.min(r - i + 1, z[i - l]);
21+
}
22+
23+
while (i + z[i] < n && s.charAt(z[i]) == s.charAt(i + z[i])) {
24+
z[i]++;
25+
}
26+
27+
if (i + z[i] - 1 > r) {
28+
l = i;
29+
r = i + z[i] - 1;
30+
}
31+
}
32+
33+
return z;
34+
}
35+
36+
public static int search(String text, String pattern) {
37+
String s = pattern + "$" + text;
38+
int[] z = zFunction(s);
39+
int p = pattern.length();
40+
41+
for (int i = 0; i < z.length; i++) {
42+
if (z[i] == p) {
43+
return i - p - 1;
44+
}
45+
}
46+
return -1;
47+
}
48+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.thealgorithms.strings;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class ZAlgorithmTest {
9+
10+
@Test
11+
void testZFunction() {
12+
int[] z = ZAlgorithm.zFunction("aaaaa");
13+
assertArrayEquals(new int[] {0, 4, 3, 2, 1}, z);
14+
}
15+
16+
@Test
17+
void testSearchFound() {
18+
assertEquals(2, ZAlgorithm.search("abcabca", "cab"));
19+
}
20+
21+
@Test
22+
void testSearchNotFound() {
23+
assertEquals(-1, ZAlgorithm.search("abcdef", "gh"));
24+
}
25+
}

0 commit comments

Comments
 (0)