File tree 1 file changed +9
-5
lines changed
1 file changed +9
-5
lines changed Original file line number Diff line number Diff line change
1
+ // Initialisation of strings algorithm Z function
2
+ // detailed review and proper examples can be seen on the link bewlow
3
+ //
4
+ // Link of algorithm review: https://www.geeksforgeeks.org/z-algorithm-linear-time-pattern-searching-algorithm/
1
5
package Strings ;
2
6
3
7
public class ZFunction {
4
- private String s ;
8
+ private String string ;
5
9
6
- public ZFunction (String s ){
7
- this .s = s ;
10
+ public ZFunction (String string ){
11
+ this .string = string ;
8
12
}
9
13
10
14
public int [] getArray (){
11
- int length = s .length ();
15
+ int length = string .length ();
12
16
int [] z = new int [length ];
13
17
int l = 0 , r = 0 ;
14
18
for (int i =0 ; i <length ; i ++){
15
19
if (i > l && i <= r ){
16
20
z [i ] = Math .min (z [i - l ], r - i + 1 );
17
21
}
18
- while (i + z [i ] < length && s .charAt (z [i ]) == s .charAt (i + z [i ])){
22
+ while (i + z [i ] < length && string .charAt (z [i ]) == string .charAt (i + z [i ])){
19
23
z [i ]++;
20
24
}
21
25
if (i + z [i ] > r ){
You can’t perform that action at this time.
0 commit comments