-
-
Notifications
You must be signed in to change notification settings - Fork 354
feat: add Levenshtein edit distance algorithm #295
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,16 @@ | ||||||
| # Edit Distance | ||||||
|
|
||||||
| Levenshtein edit distance calculates the minimum number of single-character insertions, deletions, and substitutions required to transform one string into another. | ||||||
|
|
||||||
| ``` r | ||||||
| source("../dynamic_programming/edit_distance.r") | ||||||
|
||||||
| source("../dynamic_programming/edit_distance.r") | |
| source("dynamic_programming/edit_distance.r") |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,105 @@ | ||||||||||
| # edit_distance.r | ||||||||||
| # Levenshtein edit distance algorithm in R | ||||||||||
| # Computes the minimum number of insertions, deletions, and substitutions | ||||||||||
| # required to transform one string into another. | ||||||||||
| # Time Complexity: O(m * n) | ||||||||||
| # Space Complexity: O(m * n) | ||||||||||
|
|
||||||||||
| # Compute the Levenshtein distance between two strings | ||||||||||
| edit_distance <- function(str1, str2) { | ||||||||||
| #' @param str1: First string | ||||||||||
| #' @param str2: Second string | ||||||||||
| #' @return: Integer edit distance | ||||||||||
| if (!is.character(str1) || !is.character(str2)) { | ||||||||||
| stop("Both inputs must be character strings.") | ||||||||||
| } | ||||||||||
| if (length(str1) != 1 || length(str2) != 1) { | ||||||||||
| stop("Each input must be a single string.") | ||||||||||
| } | ||||||||||
|
|
||||||||||
| m <- nchar(str1) | ||||||||||
| n <- nchar(str2) | ||||||||||
| dp <- matrix(0L, nrow = m + 1, ncol = n + 1) | ||||||||||
|
|
||||||||||
| # base cases: transform empty prefix | ||||||||||
| dp[, 1] <- seq(0L, m) | ||||||||||
| dp[1, ] <- seq(0L, n) | ||||||||||
|
|
||||||||||
| for (i in 2:(m + 1)) { | ||||||||||
| for (j in 2:(n + 1)) { | ||||||||||
|
||||||||||
| for (i in 2:(m + 1)) { | |
| for (j in 2:(n + 1)) { | |
| for (i in seq_len(m) + 1L) { | |
| for (j in seq_len(n) + 1L) { |
Copilot
AI
Apr 10, 2026
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.
Same empty-string issue as above: for (i in 2:(m + 1)) / for (j in 2:(n + 1)) can iterate invalid indices when m == 0 or n == 0, leading to out-of-bounds dp access. Please make the iteration empty-safe or handle m == 0 / n == 0 up front (and still produce a valid operations list).
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 repository already contains a Levenshtein distance implementation (
string_manipulation/levenshtein.r, also linked in DIRECTORY.md). Adding a second Levenshtein implementation under Dynamic Programming may be redundant/confusing for users; consider consolidating (extend the existing implementation to optionally return a path) or clearly differentiating this entry (e.g., mention that it returns an operation sequence).