-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathKMP Algorithm.java
More file actions
55 lines (47 loc) · 1.49 KB
/
KMP Algorithm.java
File metadata and controls
55 lines (47 loc) · 1.49 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// KMP Algorithm
// Input : txt = "abcabcdab" and pat = "abc"
// Output : true
// Explanation: pat is present in the txt
// Approach: KMP Algorithm
// KMP is most popular algorithm in string matching is the optimal algorithm
// In this algorithm we maintain the lps array to find the pattern
// Time Complexity - O(N+M)
// Space Complexity - O(M)
// Problem Link : https://www.codingninjas.com/codestudio/problems/1112621?topList=striver-sde-sheet-problems&utm_source=striver&utm_medium=website&leftPanelTab=0
// Code:
public class Solution {
public static boolean findPattern(String p, String s) {
// Write your code here.
int[] lps = new int[p.length()];
for(int i = 1;i<p.length();i++){
int j = lps[i-1];
while(j>0 && p.charAt(i) != p.charAt(j)){
j = lps[j-1];
}
if(p.charAt(i)==p.charAt(j)) j++;
if(i<p.length()) lps[i]=j;
}
int m = p.length();
int n = s.length();
int i=0;
int j=0;
while(i<n){
if(p.charAt(j)==s.charAt(i)){
i++;
j++;
}
if(j==m){
return true;
}
else if(i<n && p.charAt(j) != s.charAt(i)){
if(j==0){
i++;
}
else{
j = lps[j-1];
}
}
}
return false;
}
}