|
| 1 | +// KMP Algorithm in Java |
| 2 | +// ------------------------------------------------------ |
| 3 | +// Author: Jatin Vishwakarma |
| 4 | +// Description: Implementation of the Knuth–Morris–Pratt (KMP) |
| 5 | +// Pattern Searching Algorithm in Java. |
| 6 | +// This algorithm efficiently finds all occurrences of a pattern |
| 7 | +// in a given text with O(n + m) time complexity. |
| 8 | + |
| 9 | +import java.util.Scanner; |
| 10 | + |
| 11 | +public class KMPAlgorithm { |
| 12 | + |
| 13 | + // Function to preprocess the pattern and build the LPS (Longest Prefix Suffix) array |
| 14 | + private static void computeLPSArray(String pattern, int[] lps) { |
| 15 | + int length = 0; // length of the previous longest prefix suffix |
| 16 | + int i = 1; |
| 17 | + lps[0] = 0; // LPS of the first character is always 0 |
| 18 | + |
| 19 | + // Loop to calculate lps[i] for i = 1 to pattern.length - 1 |
| 20 | + while (i < pattern.length()) { |
| 21 | + if (pattern.charAt(i) == pattern.charAt(length)) { |
| 22 | + length++; |
| 23 | + lps[i] = length; |
| 24 | + i++; |
| 25 | + } else { |
| 26 | + if (length != 0) { |
| 27 | + // This is tricky — consider the example "AAACAAAA" |
| 28 | + length = lps[length - 1]; |
| 29 | + } else { |
| 30 | + lps[i] = 0; |
| 31 | + i++; |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // KMP Search Function |
| 38 | + public static void KMPSearch(String pattern, String text) { |
| 39 | + int M = pattern.length(); |
| 40 | + int N = text.length(); |
| 41 | + |
| 42 | + // Create LPS array |
| 43 | + int[] lps = new int[M]; |
| 44 | + computeLPSArray(pattern, lps); |
| 45 | + |
| 46 | + int i = 0; // index for text[] |
| 47 | + int j = 0; // index for pattern[] |
| 48 | + |
| 49 | + boolean found = false; |
| 50 | + |
| 51 | + while (i < N) { |
| 52 | + if (pattern.charAt(j) == text.charAt(i)) { |
| 53 | + i++; |
| 54 | + j++; |
| 55 | + } |
| 56 | + |
| 57 | + if (j == M) { |
| 58 | + System.out.println("✅ Pattern found at index: " + (i - j)); |
| 59 | + j = lps[j - 1]; |
| 60 | + found = true; |
| 61 | + } |
| 62 | + |
| 63 | + // Mismatch after j matches |
| 64 | + else if (i < N && pattern.charAt(j) != text.charAt(i)) { |
| 65 | + // Do not match lps[0..lps[j-1]] characters, they will match anyway |
| 66 | + if (j != 0) |
| 67 | + j = lps[j - 1]; |
| 68 | + else |
| 69 | + i++; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if (!found) |
| 74 | + System.out.println("❌ Pattern not found in the given text."); |
| 75 | + } |
| 76 | + |
| 77 | + public static void main(String[] args) { |
| 78 | + Scanner sc = new Scanner(System.in); |
| 79 | + |
| 80 | + System.out.println("====================================="); |
| 81 | + System.out.println(" KMP STRING MATCHING ALGORITHM"); |
| 82 | + System.out.println("====================================="); |
| 83 | + System.out.print("Enter the text: "); |
| 84 | + String text = sc.nextLine(); |
| 85 | + |
| 86 | + System.out.print("Enter the pattern to search: "); |
| 87 | + String pattern = sc.nextLine(); |
| 88 | + |
| 89 | + System.out.println("\n🔍 Searching for pattern..."); |
| 90 | + KMPSearch(pattern, text); |
| 91 | + |
| 92 | + System.out.println("\n--- Program Completed Successfully ---"); |
| 93 | + sc.close(); |
| 94 | + } |
| 95 | +} |
0 commit comments