R: find largest common substring starting at the beginning

后端 未结 11 2555
星月不相逢
星月不相逢 2021-02-19 18:33

I\'ve got 2 vectors:

word1 <- \"bestelling\"   
word2 <- \"bestelbon\"

Now I want to find the largest common substring that starts at the

11条回答
  •  闹比i
    闹比i (楼主)
    2021-02-19 18:56

    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] ""
    

提交回复
热议问题