Skip to content

Add KMP (Knuth-Morris-Pratt) Pattern Matching Algorithm in Java#591

Merged
Pradeepsingh61 merged 1 commit intoPradeepsingh61:mainfrom
Pasan11504:feature/add-kmp-algorithm-java
Oct 10, 2025
Merged

Add KMP (Knuth-Morris-Pratt) Pattern Matching Algorithm in Java#591
Pradeepsingh61 merged 1 commit intoPradeepsingh61:mainfrom
Pasan11504:feature/add-kmp-algorithm-java

Conversation

@Pasan11504
Copy link
Copy Markdown
Contributor

📋 Pull Request Description

What does this PR add?

  • New algorithm implementation
  • Data structure implementation
  • New programming language support
  • Language-specific README
  • Bug fix
  • Documentation improvement
  • Test cases

Algorithm/Feature Details

  • Name: KMP (Knuth-Morris-Pratt) Pattern Matching Algorithm
  • Language: Java
  • Category: String Algorithms
  • Time Complexity: O(n + m) where n = text length, m = pattern length
  • Space Complexity: O(m) for LPS array

Description

This PR adds a comprehensive KMP (Knuth-Morris-Pratt) Pattern Matching Algorithm implementation to Java/algorithms/string_algorithms/. KMP is an efficient string searching algorithm that avoids unnecessary comparisons by preprocessing the pattern to create an LPS (Longest Proper Prefix which is also Suffix) array.

Key Features

  • Complete Implementation: Full KMP algorithm with LPS array computation
  • Comprehensive Documentation: Detailed Javadoc explaining the algorithm, complexity, and use cases
  • 25+ Comments: Every step and design decision explained
  • Utility Methods:
    • search(text, pattern) - finds all occurrences
    • searchFirst(text, pattern) - finds first occurrence
    • contains(text, pattern) - boolean check
    • countOccurrences(text, pattern) - count matches
  • 13 Test Cases: Comprehensive testing including practical DNA matching example
  • Error Handling: Proper validation for null and empty inputs
  • Production Ready: 310 lines of clean, well-structured code

Algorithm Details

How KMP Works:

  1. Preprocessing: Computes LPS array showing longest proper prefix which is also suffix
  2. Searching: Uses LPS information to skip unnecessary comparisons
  3. Efficiency: Never backtracks in text, achieving linear time O(n+m)

Advantages over naive pattern matching:

  • Linear time complexity instead of O(n*m)
  • Never backtracks in the text string
  • Efficient for patterns with repeating sub-patterns
  • Smart shifting using preprocessing information

Real-World Use Cases:

  • Text editors (find and replace)
  • DNA sequence matching in bioinformatics
  • Plagiarism detection systems
  • Search engines and text processing
  • Network intrusion detection

Why This is Valuable

  1. Missing Algorithm: KMP was not present in Java string algorithms folder
  2. Fundamental Algorithm: One of the most important string matching algorithms
  3. Educational Value: Comprehensive documentation helps learners understand KMP
  4. Practical Examples: Includes DNA matching and real-world scenarios
  5. Production Quality: Extensive testing, error handling, and multiple utility methods

Testing

  • Code compiles without errors
  • All test cases pass (13/13)
  • Edge cases handled (empty, null, pattern > text, overlapping, etc.)
  • Error handling tested
  • Documentation complete

Test Results

=== All 13 Test Cases Passed ===
✅ Simple pattern match ✅ Multiple occurrences
✅ Overlapping patterns ✅ Pattern not found
✅ Pattern at beginning ✅ Pattern at end
✅ Single character pattern ✅ Pattern equals text
✅ DNA sequence matching ✅ Utility methods (first, contains, count)
✅ Null input handling ✅ Empty pattern handling
✅ Pattern longer than text

Quality Checklist

  • File follows naming convention: KMPAlgorithm.java (PascalCase for Java)
  • Located in correct directory: Java/algorithms/string_algorithms/
  • Algorithm description included in comprehensive Javadoc
  • Time and space complexity documented
  • 25+ meaningful comments explaining logic and LPS array
  • 13 test cases with practical examples
  • Proper input validation and error handling
  • Code is original work, not copy-pasted
  • Verified NOT a duplicate - checked upstream repository
  • Clean, readable code following Java best practices
  • Compiles and runs without errors

Verification - NOT a Duplicate

Checked upstream repository: https://github.com/Pradeepsingh61/DSA_Code/tree/main/Java/algorithms/string_algorithms
Existing files: Only Anagram, InfixToPostfix, ManacherAlgorithm, Palindrome
KMP was missing: No pattern matching algorithms existed in Java
Fills a gap: Adds fundamental string algorithm to the collection

Additional Notes

  • LPS array computation is the key optimization that makes KMP efficient
  • Implementation handles overlapping patterns correctly
  • Includes practical DNA sequence matching example
  • All utility methods follow Java conventions
  • Error messages are descriptive and helpful

Thank you for reviewing this PR! This adds a fundamental and valuable string algorithm to the repository. 🚀

#Hacktoberfest2025 #OpenSource #Java #StringAlgorithms #KMP

- Efficient string searching algorithm with O(n+m) time complexity
- Includes LPS (Longest Proper Prefix-Suffix) array preprocessing
- Comprehensive Javadoc documentation with algorithm explanation
- 25+ detailed comments explaining the logic
- 13 test cases covering all scenarios and edge cases
- Utility methods: search, searchFirst, contains, countOccurrences
- Practical examples including DNA sequence matching
- Proper error handling for null and empty inputs
- Located in Java/algorithms/string_algorithms/
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Oct 8, 2025

🎉 Welcome to Hacktoberfest 2025, @Pasan11504! 🎃

Thank you for your first contribution to our DSA repository! Here's what happens next:

🔍 Automatic Checks

  • Code Validation: Passed
  • 🧪 Compilation Tests: Passed

📋 Next Steps

🎯 Great job! Your code compiled successfully. Maintainers @Karanjot786 and @Pradeepsingh61 will review your PR soon.

🎁 What You Get

  • 🏆 Hacktoberfest Credit: This PR counts toward your 6 PR goal for exclusive T-shirt + Tree!
  • 🌟 Hall of Fame: You'll be featured in our contributors list
  • 📚 Learning: Code review feedback from experienced developers

💡 Tips for Success

  • Follow our Contributing Guidelines
  • Add comments explaining your algorithm
  • Include time/space complexity analysis
  • Test your code before submitting

Welcome to the community! 🚀

@github-actions github-actions Bot requested a review from Karanjot786 October 8, 2025 20:00
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Oct 8, 2025

🤖 Automated PR Status

🔍 Code Validation

Passed - File naming and structure look good!

🧪 Compilation Tests

Passed - All code compiles successfully!

📋 Overall Status

🎉 Ready for Review - Your PR has passed all automated checks!
👥 Maintainers have been notified for review.


This comment was generated automatically. Checks will re-run when you push new commits.

@Pradeepsingh61 Pradeepsingh61 merged commit a9d5960 into Pradeepsingh61:main Oct 10, 2025
18 checks passed
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