Jump to content

Java Implementation of the Knuth-Morris-Pratt (KMP) Algorithm for Pattern Search


shriyanshi

Recommended Posts

Hello everyone,

I wanted to share a Java implementation of the Knuth-Morris-Pratt (KMP) algorithm in java for pattern searching. The KMP algorithm is a powerful string-searching algorithm that efficiently finds occurrences of a pattern within a text.

Here's a Java code snippet that demonstrates how to implement the KMP algorithm for pattern search:

import java.util.ArrayList;
import java.util.List;

public class KMPAlgorithm {
    public static List<Integer> search(String text, String pattern) {
        List<Integer> occurrences = new ArrayList<>();
        int[] lps = computeLPSArray(pattern);
        int textIndex = 0, patternIndex = 0;

        while (textIndex < text.length()) {
            if (pattern.charAt(patternIndex) == text.charAt(textIndex)) {
                patternIndex++;
                textIndex++;
            }

            if (patternIndex == pattern.length()) {
                occurrences.add(textIndex - patternIndex);
                patternIndex = lps[patternIndex - 1];
            } else if (textIndex < text.length() && pattern.charAt(patternIndex) != text.charAt(textIndex)) {
                if (patternIndex != 0) {
                    patternIndex = lps[patternIndex - 1];
                } else {
                    textIndex++;
                }
            }
        }

        return occurrences;
    }

    private static int[] computeLPSArray(String pattern) {
        int[] lps = new int[pattern.length()];
        int len = 0;
        int i = 1;

        while (i < pattern.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++;
                }
            }
        }

        return lps;
    }

    public static void main(String[] args) {
        String text = "ABABDABACDABABCABAB";
        String pattern = "ABABCABAB";
        List<Integer> occurrences = search(text, pattern);

        if (occurrences.isEmpty()) {
            System.out.println("Pattern not found in the text.");
        } else {
            System.out.println("Pattern found at positions: " + occurrences);
        }
    }
}

Feel free to use and modify this code for your pattern-searching needs. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...