We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4e058de commit 7a4d6b2Copy full SHA for 7a4d6b2
Strings/ZFunction.java
@@ -0,0 +1,29 @@
1
+package Strings;
2
+
3
+public class ZFunction {
4
+ private String s;
5
6
+ public ZFunction(String s){
7
+ this.s = s;
8
+ }
9
10
+ public int[] getArray(){
11
+ int length = s.length();
12
+ int[] z = new int[length];
13
+ int l = 0, r = 0;
14
+ for (int i=0; i<length; i++){
15
+ if (i > l && i <= r){
16
+ z[i] = Math.min(z[i - l], r - i + 1);
17
18
+ while (i + z[i] < length && s.charAt(z[i]) == s.charAt(i + z[i])){
19
+ z[i]++;
20
21
+ if (i + z[i] > r){
22
+ l = i;
23
+ r = i + z[i] - 1;
24
25
26
27
+ return z;
28
29
+}
0 commit comments