Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 18:52 Go to next message
Ashley coder is currently offline Ashley coderFriend
Messages: 11
Registered: October 2022
Junior Member
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 06:05 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33264
Registered: July 2009
Senior Member
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.

Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Help with Preorder Traversal in C++ -
Next Topic:Java Exception Handling: Handling Checked Exceptions
Goto Forum:
  


Current Time: Tue Jan 21 16:23:46 GMT 2025

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

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

Back to the top