Add Knuth-Morris-Pratt string matching algorithm #566
Closed
+608
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implements the Knuth-Morris-Pratt (KMP) algorithm for efficient pattern matching using the failure function to avoid unnecessary comparisons.
Algorithm Features
Implementation Highlights
✅ Failure function (LPS - Longest Proper Prefix which is also Suffix)
✅ No backtracking in text - efficient for large inputs
✅ Handles overlapping matches correctly
✅ Proper null and empty input validation
✅ Descriptive exception messages
✅ Case-sensitive matching
✅ Unicode character support
✅ Additional utility methods (StartsWith, EndsWith, CountOccurrences)
Tests (34 test cases)
Code Quality
✅ Follows C# naming conventions (PascalCase)
✅ Comprehensive XML documentation
✅ StyleCop compliant
✅ No Codacy issues (no nested if statements)
✅ 100% test coverage
✅ Educational value: LPS array publicly accessible
Files Added
Algorithms/Strings/KnuthMorrisPratt.cs(213 lines)Algorithms.Tests/Strings/KnuthMorrisPrattTests.cs(395 lines)Total: 608 lines of production-quality code
Why KMP?
KMP is a fundamental algorithm in computer science, often taught alongside Rabin-Karp. While Rabin-Karp uses hashing, KMP uses a deterministic approach with the failure function, making it ideal for:
Contribution by Gittensor, learn more at https://gittensor.io/