Skip to content

Conversation

coddingjatin
Copy link
Contributor

This Java program demonstrates the Knuth–Morris–Pratt (KMP) String Matching Algorithm, an efficient technique for finding all occurrences of a substring (pattern) within a main string (text). Unlike the naive search method, KMP avoids rechecking previously matched characters by using a preprocessed Longest Prefix Suffix (LPS) array. This optimization allows it to achieve a linear time complexity of O(n + m), making it ideal for applications in text processing, compilers, and data search operations. The program takes user input for both the text and the pattern and displays all the starting indices where the pattern is found.

Copy link
Owner

@IamBisrutPyne IamBisrutPyne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @coddingjatin 👋

This is a fantastic contribution.
The KMP algorithm is a vital algorithm in CS, and a perfect fit for the Algorithms folder.

Comments

It is generally better to wrap Scanner in the try-with-resources block for resource safety.

Thank you for your contribution. Happy Hacking! ✨

@IamBisrutPyne IamBisrutPyne merged commit f95a80c into IamBisrutPyne:main Oct 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants