-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search_Pattern_KMP-Algorithm.java
66 lines (64 loc) · 3.96 KB
/
Search_Pattern_KMP-Algorithm.java
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
56
57
58
59
60
61
62
63
64
65
66
package GeeksForGeeks;
import java.util.ArrayList;
class SearchPatterKMPAlgorithm {
public static void main(String[] args) {
String txt="aecececececec",pat="ecec";
System.out.println(txt.indexOf(pat));
System.out.println(search1("ecec",txt));
System.out.println(search("ecec",txt));
System.out.println(search("ecec","aececececececdececececbbececbbdececcececdcaaacbececececacaececbececececececececebeaeaeececeececbececececadacececbaececececbececceccaaebdececbeeececaaececdbacececeececececbaececeddececacbcececacbececececececececaebaececececececcaaececececececbddececaacdadeeececceaececcececececdececdececdbcebcececbaacececaececececececececcececdeececececececbacbececaececdeebabdececacbececececeececcececbcbdececdececececaececaececeececeabececececdeaececdcdececcbeadeaaaececaecececccececcececececdececeececdececececececccececccececabececadececeddececcaececdcececececeedaececaececbcdeececcbdcaececcececececdeaececeeececececececececececaeececececececececbddececbececececdbeececcbececbcbbdececeececececececdedbdcdbaaebececdececececececececdcececaececcececcbececdabaececbeebecececdcadececbbcdececbeececdcebeececececdbbcecececcebdbccaeaccacececececdecececececececddbaeececececaecececececdeececececececedececabcbecdebecececececececececccbeececcaececedbaabececaeabecaecececaececdececececececececccacabececececececececceececaba"));
System.out.println(search1("ecec","aececececececdececececbbececbbdececcececdcaaacbececececacaececbececececececececebeaeaeececeececbececececadacececbaececececbececceccaaebdececbeeececaaececdbacececeececececbaececeddececacbcececacbececececececececaebaececececececcaaececececececbddececaacdadeeececceaececcececececdececdececdbcebcececbaacececaececececececececcececdeececececececbacbececaececdeebabdececacbececececeececcececbcbdececdececececaececaececeececeabececececdeaececdcdececcbeadeaaaececaecececccececcececececdececeececdececececececccececccececabececadececeddececcaececdcececececeedaececaececbcdeececcbdcaececcececececdeaececeeececececececececececaeececececececececbddececbececececdbeececcbececbcbbdececeececececececdedbdcdbaaebececdececececececececdcececaececcececcbececdabaececbeebecececdcadececbbcdececbeececdcebeececececdbbcecececcebdbccaeaccacececececdecececececececddbaeececececaecececececdeececececececedececabcbecdebecececececececececccbeececcaececedbaabececaeabecaecececaececdececececececececccacabececececececececceececaba"));
}
static ArrayList<Integer> search1(String pat, String txt)
{
ArrayList<Integer> result= new ArrayList<>();
int found = txt.indexOf(pat);
while(found!=-1){
result.add(found+1);
found= txt.indexOf(pat,found+1);
}
return result;
}
static ArrayList<Integer> search(String pat, String txt)
{
ArrayList<Integer> result= new ArrayList<>();
int patternLength=pat.length();
for(int i=0;i<txt.length();i++){
if(txt.charAt(i)==pat.charAt(0)){
if(i+patternLength>txt.length()){
return result;
}
String subPattern=txt.substring(i,i+patternLength);
if(subPattern.equals(pat)){
result.add(i+1);
// i=i+patternLength-1;
}
}
}
return result;
}
// static ArrayList<Integer> search(String pat, String txt)
// {
// ArrayList<Integer> result= new ArrayList<>();
// int j=0,index = 0;
// for(int i=0;i<txt.length();i++){
// if(txt.charAt(i)==pat.charAt(j)){
// if(j==0){
// index=i+1;
// }
// j++;
// }else if(txt.charAt(i)==pat.charAt(0)) {
// index=i+1;
// j=1;
// }else {
// j=0;
// index=0;
// }
// if(j==pat.length()){
// j=0;
// result.add(index);
// }
// }
// return result;
// }
}