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
This commit is contained in:
utkarsh patel
2025-12-14 15:38:18 +05:30
committed by GitHub
parent 746457ce15
commit bccaf970ba
3 changed files with 75 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
package com.thealgorithms.strings;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class ZAlgorithmTest {
@Test
void testZFunction() {
int[] z = ZAlgorithm.zFunction("aaaaa");
assertArrayEquals(new int[] {0, 4, 3, 2, 1}, z);
}
@Test
void testSearchFound() {
assertEquals(2, ZAlgorithm.search("abcabca", "cab"));
}
@Test
void testSearchNotFound() {
assertEquals(-1, ZAlgorithm.search("abcdef", "gh"));
}
}