-
-
Notifications
You must be signed in to change notification settings - Fork 342
feat: Add Rabin–Karp string search algorithm in R #166
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
Merged
siriak
merged 4 commits into
TheAlgorithms:master
from
sgindeed:feat/rabin-karp-sgindeed
Oct 8, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4b4f746
feat: Add Rabin–Karp string search algorithm in R
sgindeed b72730f
fix: handle edge cases in Rabin–Karp algorithm (empty pattern, single…
sgindeed aed5d86
Merge branch 'master' into feat/rabin-karp-sgindeed
sgindeed 3a6116c
Update searches/rabin.karp.string.search.R
siriak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # Rabin–Karp String Search Algorithm in R | ||
| # Author: sgindeed | ||
| # Description: Finds all occurrences of a pattern in a given text using a rolling hash technique. | ||
|
|
||
| # Ask user for input | ||
| text <- readline(prompt = "Enter the text: ") | ||
| pattern <- readline(prompt = "Enter the pattern to search: ") | ||
|
|
||
| # Convert both to lowercase for case-insensitive matching | ||
| text <- tolower(text) | ||
| pattern <- tolower(pattern) | ||
|
|
||
| # Get lengths | ||
| n <- nchar(text) | ||
| m <- nchar(pattern) | ||
|
|
||
| # Handle empty or invalid inputs | ||
| if (m == 0) { | ||
| cat("Empty pattern. Nothing to search.\n") | ||
| quit(save = "no") | ||
| } | ||
|
|
||
| if (m > n) { | ||
| cat("Pattern is longer than text. Pattern not found in text.\n") | ||
| quit(save = "no") | ||
| } | ||
|
|
||
| # Constants | ||
| base <- 256 # Number of possible characters | ||
| mod <- 101 # A prime number for hashing | ||
|
|
||
| # Initialize variables | ||
| p_hash <- 0 # hash for pattern | ||
| t_hash <- 0 # hash for text window | ||
| h <- 1 # base^(m-1) | ||
| matches <- c() | ||
|
|
||
| # Compute (base^(m-1)) % mod safely | ||
| for (i in seq_len(m - 1)) { | ||
| h <- (h * base) %% mod | ||
| } | ||
|
|
||
| # Convert characters to ASCII values | ||
| pattern_chars <- utf8ToInt(pattern) | ||
| text_chars <- utf8ToInt(text) | ||
|
|
||
| # Compute initial hash values for pattern and first window of text | ||
| for (i in 1:m) { | ||
| p_hash <- (base * p_hash + pattern_chars[i]) %% mod | ||
| t_hash <- (base * t_hash + text_chars[i]) %% mod | ||
| } | ||
|
|
||
| # Rabin–Karp main search | ||
| for (i in 0:(n - m)) { | ||
| # If hash matches, verify actual substring | ||
| if (p_hash == t_hash) { | ||
| if (substr(text, i + 1, i + m) == pattern) { | ||
| matches <- c(matches, i + 1) | ||
siriak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| # Slide window: remove first char, add next char | ||
| if (i < n - m) { | ||
| t_hash <- (base * (t_hash - text_chars[i + 1] * h) + text_chars[i + m + 1]) %% mod | ||
siriak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (t_hash < 0) { | ||
| t_hash <- t_hash + mod | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Display results | ||
| if (length(matches) > 0) { | ||
| cat("Pattern found at positions:", paste(matches, collapse = ", "), "\n") | ||
| } else { | ||
| cat("Pattern not found in the given text.\n") | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.