I\'ve got 2 vectors:
word1 <- \"bestelling\"
word2 <- \"bestelbon\"
Now I want to find the largest common substring that starts at the
As much as I generally avoid a for loop in R - given you're starting at the beginning and continuing until you find the solution it seemed an easy approach.
It's a bit more intuitive than some of the other examples I think
lcsB <- function(string1, string2) {
x <- ''
for (i in 1:nchar(string1)){
if (substr(string1[1],1,i) == substr(string2[1],1,i)) {
x <- substr(string1[1],1,i)
}
else
return(x)
}
return(x)
}
lcsB("bestelling", "bestelbon")
lcsB("bestelling", "stel")