-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMP_String_Matching.java
More file actions
77 lines (63 loc) · 2.25 KB
/
KMP_String_Matching.java
File metadata and controls
77 lines (63 loc) · 2.25 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.util.Scanner;
public class KMP_String_Matching {
void searchPattern(String pattern, String text) {
int patternLength = pattern.length();
int textLength = text.length();
int[] lps = new int[patternLength];
computeLPSArray(pattern, patternLength, lps);
int i = 0;
int j = 0;
while (i < textLength) {
if (pattern.charAt(j) == text.charAt(i)) {
i++;
j++;
}
if (j == patternLength) {
System.out.println("Pattern found at index " + (i - j));
j = lps[j - 1];
} else if (i < textLength && pattern.charAt(j) != text.charAt(i)) {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
}
void computeLPSArray(String pattern, int length, int[] lps) {
int len = 0;
lps[0] = 0;
int i = 1;
while (i < length) {
if (pattern.charAt(i) == pattern.charAt(len)) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the text to search in: ");
String text = scanner.nextLine();
System.out.print("Enter the pattern to search for: ");
String pattern = scanner.nextLine();
KMP_String_Matching kmp = new KMP_String_Matching();
kmp.searchPattern(pattern, text);
}
}
// Step 1: Ask the user to enter the main text and the pattern to search for
// Step 2: Create an array (lps) to store the longest prefix that is also a suffix
// Step 3: Fill this lps array using a helper method
// Step 4: Start comparing characters of pattern and text
// Step 5: If characters match, move forward
// Step 6: If full pattern matches, print the index and continue
// Step 7: If characters don't match, use lps to skip comparisons
// Step 8: Repeat until the whole text is checked