mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 20:20:56 +08:00
description added and also link to the algorithm review
This commit is contained in:
@ -1,21 +1,25 @@
|
||||
// Initialisation of strings algorithm Z function
|
||||
// detailed review and proper examples can be seen on the link bewlow
|
||||
//
|
||||
// Link of algorithm review: https://www.geeksforgeeks.org/z-algorithm-linear-time-pattern-searching-algorithm/
|
||||
package Strings;
|
||||
|
||||
public class ZFunction {
|
||||
private String s;
|
||||
private String string;
|
||||
|
||||
public ZFunction(String s){
|
||||
this.s = s;
|
||||
public ZFunction(String string){
|
||||
this.string = string;
|
||||
}
|
||||
|
||||
public int[] getArray(){
|
||||
int length = s.length();
|
||||
int length = string.length();
|
||||
int[] z = new int[length];
|
||||
int l = 0, r = 0;
|
||||
for (int i=0; i<length; i++){
|
||||
if (i > l && i <= r){
|
||||
z[i] = Math.min(z[i - l], r - i + 1);
|
||||
}
|
||||
while (i + z[i] < length && s.charAt(z[i]) == s.charAt(i + z[i])){
|
||||
while (i + z[i] < length && string.charAt(z[i]) == string.charAt(i + z[i])){
|
||||
z[i]++;
|
||||
}
|
||||
if (i + z[i] > r){
|
||||
|
Reference in New Issue
Block a user