-
-
Notifications
You must be signed in to change notification settings - Fork 331
Add Knuth-Morris-Pratt (KMP) string matching algorithm [HACKTOBERFEST 2025] #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add Knuth-Morris-Pratt (KMP) string matching algorithm [HACKTOBERFEST 2025] #157
Conversation
- Implement efficient O(n+m) string pattern matching - Include failure function computation with detailed explanation - Add multiple search variants (first occurrence, count, all matches) - Performance comparison with naive string matching - Comprehensive examples including DNA sequence analysis - Create String Manipulation section in DIRECTORY.md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements the Knuth-Morris-Pratt (KMP) string matching algorithm in R, providing an efficient O(n+m) solution for finding pattern occurrences in text. The implementation includes educational features, performance comparisons, and real-world application examples.
- Core KMP algorithm with failure function preprocessing
- Multiple search variants (find all, find first, count occurrences)
- Educational tools including failure function visualization and performance benchmarking
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
string_manipulation/kmp_string_matching.r | Complete KMP algorithm implementation with multiple search functions, performance testing, and educational examples |
DIRECTORY.md | Added entry for the new KMP string matching algorithm |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
DIRECTORY.md
Outdated
## String Manipulation | ||
* [KMP String Matching](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/kmp_string_matching.r) | ||
|
||
``` |
Copilot
AI
Oct 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The closing code block fence ``` on line 91 is not valid Markdown syntax here. This appears to be a stray code fence that should be removed.
``` |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Test 3: Edge cases | ||
cat("3. Edge Cases\n") | ||
cat("Empty pattern:", length(kmp_search("hello", "")), "matches\n") | ||
cat("Empty text:", length(kmp_search("", "hello")), "matches\n") |
Copilot
AI
Oct 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's trailing whitespace at the end of this line that should be removed for consistency.
cat("Empty text:", length(kmp_search("", "hello")), "matches\n") | |
cat("Empty text:", length(kmp_search("", "hello")), "matches\n") |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still an issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you resolving this comment without applying the change or replying to the comment? It's a valid issue that must be fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@siriak Thank you for the feedback! I've addressed the issues you mentioned:
✅ Fixed DIRECTORY.md formatting:
- Corrected the Wiggle Sort indentation (was 4 spaces, now 2 spaces like other entries)
- Cleaned up formatting issues
✅ Enhanced KMP edge case testing:
- Added tests for identical strings
- Added tests for pattern at start/end positions
- Improved output formatting with consistent array display
The formatting and test coverage issues you identified have been resolved. Please review the latest commit.
Please check comments |
@siriak all conflicts resolved |
# Test 3: Edge cases | ||
cat("3. Edge Cases\n") | ||
cat("Empty pattern:", length(kmp_search("hello", "")), "matches\n") | ||
cat("Empty text:", length(kmp_search("", "hello")), "matches\n") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you resolving this comment without applying the change or replying to the comment? It's a valid issue that must be fixed
- Fix indentation issue with Wiggle Sort entry (was 4 spaces, now 2 spaces) - Remove extra trailing content and formatting issues - Add comprehensive edge case tests for KMP algorithm - Include tests for identical strings, pattern at start/end positions - Address reviewer feedback about missing test cases and formatting This addresses @siriak's valid concerns about proper formatting and test coverage.
0d2c157
to
2908f89
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.
@@ -0,0 +1,343 @@ | |||
# Knuth-Morris-Pratt (KMP) String Matching Algorithm |
Copilot
AI
Oct 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File extension should be lowercase '.r' instead of '.r'. The current filename 'kmp_string_matching.r' already follows the correct convention.
Copilot generated this review using guidance from repository custom instructions.
if (naive_time > 0 && kmp_time > 0) { | ||
speedup <- naive_time / kmp_time | ||
cat("KMP speedup:", sprintf("%.2f", speedup), "x faster\n") | ||
} |
Copilot
AI
Oct 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The performance comparison may be unreliable for very fast operations. Consider using microbenchmark
package or running multiple iterations to get more accurate timing measurements.
Copilot uses AI. Check for mistakes.
Description: