File tree Expand file tree Collapse file tree 1 file changed +10
-3
lines changed
src/main/java/com/thealgorithms/strings Expand file tree Collapse file tree 1 file changed +10
-3
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * https://en.wikipedia.org/wiki/Z-algorithm
3+ */
4+
15package com .thealgorithms .strings ;
26
37public class ZAlgorithm {
@@ -7,8 +11,10 @@ public static int[] zFunction(String s) {
711 int [] z = new int [n ];
812 int l = 0 , r = 0 ;
913 for (int i = 1 ; i < n ; i ++) {
10- if (i <= r ) z [i ] = Math .min (r - i + 1 , z [i - l ]);
11- while (i + z [i ] < n && s .charAt (z [i ]) == s .charAt (i + z [i ])) z [i ]++;
14+ if (i <= r )
15+ z [i ] = Math .min (r - i + 1 , z [i - l ]);
16+ while (i + z [i ] < n && s .charAt (z [i ]) == s .charAt (i + z [i ]))
17+ z [i ]++;
1218 if (i + z [i ] - 1 > r ) {
1319 l = i ;
1420 r = i + z [i ] - 1 ;
@@ -22,7 +28,8 @@ public static int search(String text, String pattern) {
2228 int [] z = zFunction (s );
2329 int p = pattern .length ();
2430 for (int i = 0 ; i < z .length ; i ++) {
25- if (z [i ] == p ) return i - p - 1 ;
31+ if (z [i ] == p )
32+ return i - p - 1 ;
2633 }
2734 return -1 ;
2835 }
You can’t perform that action at this time.
0 commit comments