Created
November 26, 2014 11:56
-
-
Save alistairstead3408/2017fac04f33cc0d663e to your computer and use it in GitHub Desktop.
This file contains 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
#Takes two character vectors | |
lev_dist_better <- function(s, t){ | |
if(identical(s, t)) return(0) | |
if(length(s) == 0) return(length(t)) | |
if(length(t) == 0) return(length(s)) | |
#initialise v0 with a sequence from 0 to length of t + 1 | |
previous <- seq(1, length(t) + 1, 1) | |
current <- c(rep(0, length(t) + 1)) | |
for(i in 1:length(s)){ | |
current[1] <- i +1 | |
for(j in 1:length(t)){ | |
cost <- ifelse(s[i] == t[j], 0, 1) | |
current[j + 1] = min(current[j] + 1, previous[j+1] + 1, previous[j] + cost) | |
} | |
for(j in 1:length(previous)) | |
previous[j] = current[j] | |
} | |
return(current[length(t)+1]-1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment