Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 62 additions & 22 deletions string_manipulation/levenshtein.r
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(
Comment thread
siriak marked this conversation as resolved.
Resolved
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()) {
Comment thread
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 = ""
)
}
Loading