forked from Itachi-Ucchiha/BasicDSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10thJune(II).java
More file actions
35 lines (31 loc) · 846 Bytes
/
Copy path10thJune(II).java
File metadata and controls
35 lines (31 loc) · 846 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
public class KMP_Pattern_matching {
public static void main(String[] args) {
String str = "batmanandrobinarebat";
String pattern = "bat";
int n = str.length();
int p = pattern.length();
int []lps = new int[n];
int i = 0;
int j = 0;
while(i<n){
if(pattern.charAt(j)==str.charAt(i)){
i++;
j++;
}
if(j==p){
System.out.println("Pattern Found at: "+(i-j));
j = lps[j-1];
// j = 0;
}
else if(i<n && pattern.charAt(j)!=str.charAt(i)){
if(j==0){
i++;
}
else{
j = lps[j-1];
// j = 0;
}
}
}
}
}