-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain5.java
More file actions
39 lines (37 loc) · 954 Bytes
/
Main5.java
File metadata and controls
39 lines (37 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.*;
// import practice_ques.bit_manipulation.removeLastBit;
public class Main5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = "ABCABDABCABCABD";
int n = s.length();
int[] lps = new int[n];
System.out.println(Arrays.toString(kmp(s,lps)));
}
static int[] kmp(String s,int[] lps){
int l = 0;
int r = 1;
int n = s.length();
while(r<n){
if(s.charAt(l)==s.charAt(r)){
lps[r] = l+1;
l+=1;
r+=1;
}
else if(l==0){
r+=1;
}
else{
if(lps[l-1]==0){
lps[r] = 0;
r+=1;
l = 0;
}
else{
l = lps[l-1];
}
}
}
return lps;
}
}