I\'ve got 2 vectors:
word1 <- \"bestelling\"
word2 <- \"bestelbon\"
Now I want to find the largest common substring that starts at the
fun <- function(words) {
#extract substrings from length 1 to length of shortest word
subs <- sapply(seq_len(min(nchar(words))),
function(x, words) substring(words, 1, x),
words=words)
#max length for which substrings are equal
neqal <- max(cumsum(apply(subs, 2, function(x) length(unique(x)) == 1L)))
#return substring
substring(words[1], 1, neqal)
}
words1 <- c("bestelling", "bestelbon")
fun(words1)
#[1] "bestel"
words2 <- c("bestelling", "stel")
fun(words2)
#[1] ""