File tree Expand file tree Collapse file tree
src/main/java/com/thealgorithms/strings Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -11,35 +11,35 @@ private ZAlgorithm() {
1111
1212 public static int [] zFunction (String s ) {
1313 int n = s .length ();
14- int [] z = new int [n ];
15- int l = 0 ;
16- int r = 0 ;
14+ int [] zArray = new int [n ];
15+ int leftBound = 0 ;
16+ int rightBound = 0 ;
1717
1818 for (int i = 1 ; i < n ; i ++) {
19- if (i <= r ) {
20- z [i ] = Math .min (r - i + 1 , z [i - l ]);
19+ if (i <= rightBound ) {
20+ zArray [i ] = Math .min (rightBound - i + 1 , zArray [i - leftBound ]);
2121 }
2222
23- while (i + z [i ] < n && s .charAt (z [i ]) == s .charAt (i + z [i ])) {
24- z [i ]++;
23+ while (i + zArray [i ] < n && s .charAt (zArray [i ]) == s .charAt (i + zArray [i ])) {
24+ zArray [i ]++;
2525 }
2626
27- if (i + z [i ] - 1 > r ) {
28- l = i ;
29- r = i + z [i ] - 1 ;
27+ if (i + zArray [i ] - 1 > rightBound ) {
28+ leftBound = i ;
29+ rightBound = i + zArray [i ] - 1 ;
3030 }
3131 }
3232
33- return z ;
33+ return zArray ;
3434 }
3535
3636 public static int search (String text , String pattern ) {
3737 String s = pattern + "$" + text ;
38- int [] z = zFunction (s );
38+ int [] zArray = zFunction (s );
3939 int p = pattern .length ();
4040
41- for (int i = 0 ; i < z .length ; i ++) {
42- if (z [i ] == p ) {
41+ for (int i = 0 ; i < zArray .length ; i ++) {
42+ if (zArray [i ] == p ) {
4343 return i - p - 1 ;
4444 }
4545 }
You can’t perform that action at this time.
0 commit comments