Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Understanding the KMP Algorithm for Efficient Pattern Searching in Java
Understanding the KMP Algorithm for Efficient Pattern Searching in Java [message #1861253] Mon, 02 October 2023 14:52 Go to next message
Eclipse UserFriend
Hey everyone,

I recently came across an interesting algorithm called the Knuth-Morris-Pratt (KMP) algorithm in Java for pattern searching. I found a great explanation and example implementation in Java online. Let's dive into it and explore the power of the KMP algorithm together!

Problem Task:

Implement the KMP algorithm in Java based on the explanation provided in the post.

Java Code Implementation:

public class KMPAlgorithm {

    public static void KMPSearch(String text, String pattern) {
        int[] lps = computeLPSArray(pattern);
        int i = 0;  // Index for text[]
        int j = 0;  // Index for pattern[]

        while (i < text.length()) {
            if (pattern.charAt(j) == text.charAt(i)) {
                i++;
                j++;
            }
            if (j == pattern.length()) {
                System.out.println("Pattern found at index " + (i - j));
                j = lps[j - 1];
            } else if (i < text.length() && pattern.charAt(j) != text.charAt(i)) {
                if (j != 0) {
                    j = lps[j - 1];
                } else {
                    i++;
                }
            }
        }
    }

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

        while (i < pattern.length()) {
            if (pattern.charAt(i) == pattern.charAt(length)) {
                length++;
                lps[i] = length;
                i++;
            } else {
                if (length != 0) {
                    length = lps[length - 1];
                } else {
                    lps[i] = 0;
                    i++;
                }
            }
        }
        return lps;
    }

    public static void main(String[] args) {
        String text = "ABABDABACDABABCABAB";
        String pattern = "ABABCABAB";
        System.out.println("Text: " + text);
        System.out.println("Pattern: " + pattern);
        System.out.println("Indices where pattern found:");
        KMPSearch(text, pattern);
    }
}



Re: Understanding the KMP Algorithm for Efficient Pattern Searching in Java [message #1861259 is a reply to message #1861253] Tue, 03 October 2023 02:05 Go to previous message
Eclipse UserFriend
You seem to be posting to a lot of places with "interesting" things to say. Mostly people who do that will want to post links to something not really so relevant. There are lots of links to www-interviewbit-com which I think is the only point to your posts. Could you please stop doing that? If you don't I'll ask the webmaster to block your account and delete all your posts.
Previous Topic:Help with Preorder Traversal in C++ -
Next Topic:Java Exception Handling: Handling Checked Exceptions
Goto Forum:
  


Current Time: Sat Aug 30 21:52:18 EDT 2025

Powered by FUDForum. Page generated in 0.03734 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top