我有2个向量:
word1 <- "bestelling"
word2 <- "bestelbon"
现在,我想找到以beginnig开始的最大公共子字符串,因此此处为“ bestel”。
但是以其他两个词为例,例如“ bestelling”和“ stel”,那么我想返回""
。
这将适用于任意单词向量
words <- c('bestelling', 'bestelbon')
words.split <- strsplit(words, '')
words.split <- lapply(words.split, `length<-`, max(nchar(words)))
words.mat <- do.call(rbind, words.split)
common.substr.length <- which.max(apply(words.mat, 2, function(col) !length(unique(col)) == 1)) - 1
substr(words[1], 1, common.substr.length)
# [1] "bestel"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句