This is similar to Stars and bars. Here, our letters are analogous to the stars and the dots are analogous to the bars:
## transform string into stars and bars format
ab
## add spaces around each letter
_a_b_
## substitute stars (i.e. asterisks) for letters
_*_*_
Now, we simply ask the question:
Given n spaces, how many ways can we fill those spaces with 0 to n bars?
As @Gregor pointed out, this turns out to be the sum of the binomial coefficients (assuming n letters):
sum(sapply(0:(n + 1), function(x) combinat::nCm(n + 1, x))) == 2^(n + 1)
With base R we can easily achieve the desired result:
myStr <- "abcd"
indStr <- strsplit(myStr, split = "")[[1]]
strTemplate <- vector("character", length = (length(indStr) * 2 + 1))
strTemplate[seq(2, length(strTemplate), 2)] <- indStr
strTemplate
[1] "" "a" "" "b" "" "c" "" "d" ""
dotVec <- seq(1L, length(strTemplate), 2L)
dotVec
[1] 1 3 5 7 9
unlist(lapply(1:length(dotVec), function(x) {
combn(dotVec, x, FUN = function(y) {
temp <- strTemplate
temp[y] <- "."
paste0(temp, collapse = "")
})
}))
[1] ".abcd" "a.bcd" "ab.cd" "abc.d" "abcd."
[6] ".a.bcd" ".ab.cd" ".abc.d" ".abcd." "a.b.cd"
[11] "a.bc.d" "a.bcd." "ab.c.d" "ab.cd." "abc.d."
[16] ".a.b.cd" ".a.bc.d" ".a.bcd." ".ab.c.d" ".ab.cd."
[21] ".abc.d." "a.b.c.d" "a.b.cd." "a.bc.d." "ab.c.d."
[26] ".a.b.c.d" ".a.b.cd." ".a.bc.d." ".ab.c.d." "a.b.c.d."
[31] ".a.b.c.d."