R:从头开始寻找最大的公共子串 [英] R: find largest common substring starting at the beginning
本文介绍了R:从头开始寻找最大的公共子串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 2 个向量:
word1 <- "bestelling"
word2 <- "bestelbon"
现在我想找到从 beginnig 开始的最大公共子串,所以这里就是bestel".
Now I want to find the largest common substring that starts at the beginnig, so here that would be "bestel".
但以bestelling"和stel"这两个词为例,那么我想返回""
.
But take for example two other words like "bestelling" and "stel", then I want to return ""
.
推荐答案
这适用于任意词向量
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"
这篇关于R:从头开始寻找最大的公共子串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文