Find all possible substrings of length n

I have an interesting (only for me, perhaps :)) question. I have text like:

"abbba"

The question is to find all possible substrings of length n in this string. For example, if n = 2, substrings

'ab','bb','ba'

and if n = 3, substrings

'abb','bbb','bba'

I was thinking of using something like this:

x <- 'abbba'
m <- matrix(strsplit(x, '')[[1]], nrow=2)
apply(m, 2, paste, collapse='')

But I have a warning, and it does not work for len = 3.

+4
source share
2 answers

With combnall combinations of the vector will be sorted by column. Separating the vector before and transferring the result will give the result in the form of a matrix. Then it can be combined with do.call(paste,...)on the matrix as a data frame:

mat <- unique(t(combn(strsplit(x, "")[[1]],2)))
do.call(paste0, as.data.frame(mat))
#[1] "ab" "aa" "bb" "ba"

Update

combn (@docendo):

unique(combn(strsplit(x, "")[[1]],3, FUN=paste, collapse=""))

, all. , .

+2
x <- "abbba"
allsubstr <- function(x, n) unique(substring(x, 1:(nchar(x) - n + 1), n:nchar(x)))
allsubstr(x, 2)
# [1] "ab" "bb" "ba"
allsubstr(x, 3)
# [1] "abb" "bbb" "bba"
+8

Source: https://habr.com/ru/post/1629931/