Repository navigation
Fix Levenshtein distance for empty strings #301
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 1 commit into
TheAlgorithms:master
from
Navanee45:fix-levenshtein-empty-strings
Nov 18, 2026
Merged
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -1,38 +1,78 @@ | ||
| # Levenshtein Distance in R | ||
| # Calculates minimum number of edits (insert, delete, replace) | ||
| # to convert one string into another and prints the distance. | ||
| # | ||
| # Calculates the minimum number of edits needed to transform | ||
| # one string into another. | ||
| # | ||
| # Allowed operations: | ||
| # - insertion | ||
| # - deletion | ||
| # - substitution | ||
|
|
||
| levenshtein_distance <- function(s1, s2) { | ||
| if (length(s1) != 1 || length(s2) != 1) { | ||
| stop("s1 and s2 must each contain exactly one string") | ||
| } | ||
|
|
||
| s1 <- as.character(s1) | ||
| s2 <- as.character(s2) | ||
|
|
||
| n <- nchar(s1) | ||
| m <- nchar(s2) | ||
|
|
||
| # Initialize DP matrix | ||
| dp <- matrix(0, nrow = n + 1, ncol = m + 1) | ||
| dp <- matrix( | ||
| 0, | ||
| nrow = n + 1, | ||
| ncol = m + 1 | ||
| ) | ||
|
|
||
| for (i in 0:n) dp[i + 1, 1] <- i | ||
| for (j in 0:m) dp[1, j + 1] <- j | ||
| for (i in 0:n) { | ||
| dp[i + 1, 1] <- i | ||
| } | ||
|
|
||
| # Fill DP table | ||
| for (i in 1:n) { | ||
| for (j in 1:m) { | ||
| if (substr(s1, i, i) == substr(s2, j, j)) { | ||
| dp[i + 1, j + 1] <- dp[i, j] | ||
| } else { | ||
| for (j in 0:m) { | ||
| dp[1, j + 1] <- j | ||
| } | ||
|
|
||
| # Avoid 1:0 when either string is empty. | ||
| if (n > 0 && m > 0) { | ||
| for (i in seq_len(n)) { | ||
| for (j in seq_len(m)) { | ||
| substitution_cost <- if ( | ||
| substr(s1, i, i) == substr(s2, j, j) | ||
| ) { | ||
| 0 | ||
| } else { | ||
| 1 | ||
| } | ||
|
|
||
| dp[i + 1, j + 1] <- min( | ||
| dp[i, j + 1] + 1, # Deletion | ||
| dp[i + 1, j] + 1, # Insertion | ||
| dp[i, j] + 1 # Substitution | ||
| dp[i, j + 1] + 1, | ||
| dp[i + 1, j] + 1, | ||
| dp[i, j] + substitution_cost | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return(dp[n + 1, m + 1]) | ||
| dp[n + 1, m + 1] | ||
| } | ||
|
|
||
| # Interactive input | ||
| s1 <- tolower(readline("Enter first string: ")) | ||
| s2 <- tolower(readline("Enter second string: ")) | ||
|
|
||
| distance <- levenshtein_distance(s1, s2) | ||
| cat("Levenshtein distance between '", s1, "' and '", s2, "' is:", distance, "\n") | ||
| # Interactive example. | ||
| # This does not run when the file is sourced for testing. | ||
| if (interactive()) { | ||
|
siriak marked this conversation as resolved.
Resolved
|
||
| s1 <- tolower(readline("Enter first string: ")) | ||
| s2 <- tolower(readline("Enter second string: ")) | ||
|
|
||
| distance <- levenshtein_distance(s1, s2) | ||
|
|
||
| cat( | ||
| "Levenshtein distance between '", | ||
| s1, | ||
| "' and '", | ||
| s2, | ||
| "' is: ", | ||
| distance, | ||
| "\n", | ||
| sep = "" | ||
| ) | ||
| } | ||
Oops, something went wrong.
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.