description added and also link to the algorithm review

This commit is contained in:
vakhokoto
2020-07-22 12:52:54 +04:00
parent 7a4d6b2544
commit a983a15b62

View File

@ -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; package Strings;
public class ZFunction { public class ZFunction {
private String s; private String string;
public ZFunction(String s){ public ZFunction(String string){
this.s = s; this.string = string;
} }
public int[] getArray(){ public int[] getArray(){
int length = s.length(); int length = string.length();
int[] z = new int[length]; int[] z = new int[length];
int l = 0, r = 0; int l = 0, r = 0;
for (int i=0; i<length; i++){ for (int i=0; i<length; i++){
if (i > l && i <= r){ if (i > l && i <= r){
z[i] = Math.min(z[i - l], r - i + 1); 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]++; z[i]++;
} }
if (i + z[i] > r){ if (i + z[i] > r){