I\'ve got 2 vectors:
word1 <- \"bestelling\"
word2 <- \"bestelbon\"
Now I want to find the largest common substring that starts at the
why not add another! and hack at it so the answer is different than everyone elses!
largestStartSubstr<-function(word1, word2){
word1vec<-unlist(strsplit(word1, "", fixed=TRUE))
word2vec<-unlist(strsplit(word2, "", fixed=TRUE))
indexes<-intersect(1:nchar(word1), 1:nchar(word2))
bools<-word1vec[indexes]==word2vec[indexes]
if(bools[1]==FALSE){
""
}else{
lastChar<-match(1,c(0,diff(cumsum(!bools))))-1
if(is.na(lastChar)){
lastChar<-indexes[length(indexes)]
}
substr(word1, 1,lastChar)
}
}
word1 <- "bestselling"
word2<- "bestsel"
largestStartSubstr(word1, word2)
#[1] "bestsel"
word1 <- "bestselling"
word2<- "sel"
largestStartSubstr(word1, word2)
#[1] ""