diff --git a/src/main/java/com/thealgorithms/KMP.java b/src/main/java/com/thealgorithms/KMP.java new file mode 100644 index 000000000000..46f35e6d5fd3 --- /dev/null +++ b/src/main/java/com/thealgorithms/KMP.java @@ -0,0 +1,60 @@ +public class KMP { + + // Function to compute LPS array + public static int[] computeLPSArray(String pattern) { + int M = pattern.length(); + int[] lps = new int[M]; + int len = 0; // length of the previous longest prefix suffix + int i = 1; + lps[0] = 0; // lps[0] is always 0 + + while (i < M) { + 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++; + } + } + } + return lps; + } + + // KMP Search function + public static void KMPSearch(String pattern, String text) { + int M = pattern.length(); + int N = text.length(); + + int[] lps = computeLPSArray(pattern); + int i = 0; // index for text + int j = 0; // index for pattern + + while (i < N) { + if (pattern.charAt(j) == text.charAt(i)) { + i++; + j++; + } + + if (j == M) { + System.out.println("Pattern found at index " + (i - j)); + j = lps[j - 1]; + } else if (i < N && pattern.charAt(j) != text.charAt(i)) { + if (j != 0) + j = lps[j - 1]; + else + i++; + } + } + } + + public static void main(String[] args) { + String text = "ABABDABACDABABCABAB"; + String pattern = "ABABCABAB"; + KMPSearch(pattern, text); + } +}