RoyalTS
RoyalTS

Reputation: 10203

all strings of length k that can be formed from a set of n characters

This question has been asked for other languages but I'm looking for the most idiomatic way to find all strings of length k that can be formed from a set of n characters in R

Example input and output:

input <- c('a', 'b')

output <- c('aa', 'ab', 'ba', 'bb')

Upvotes: 4

Views: 97

Answers (4)

Ben Bolker
Ben Bolker

Reputation: 226332

A little more complicated than I'd like. I think outer() only works for n=2. combn doesn't include repeats.

allcomb <- function(input = c('a', 'b'), n=2) {
  args <- rep(list(input),n)
  gr <- do.call(expand.grid,args)
  return(do.call(paste0,gr))
}

Thanks to @thelatemail for improvements ...

allcomb(n=4)
## [1] "aaaa" "baaa" "abaa" "bbaa" "aaba" "baba" "abba"
## [8] "bbba" "aaab" "baab" "abab" "bbab" "aabb" "babb"
## [15] "abbb" "bbbb"

Upvotes: 6

Jonathan Carroll
Jonathan Carroll

Reputation: 3947

I'm not proud of how this looks, but it works...

allcombs <- function(x, k) {

  apply(expand.grid(split(t(replicate(k, x)), seq_len(k))), 1, paste, collapse = "")

}

allcombs(letters[1:2], 2)
#> [1] "aa" "ba" "ab" "bb"

allcombs(letters[1:2], 4)
#>  [1] "aaaa" "baaa" "abaa" "bbaa" "aaba" "baba" "abba" "bbba" "aaab" "baab"
#> [11] "abab" "bbab" "aabb" "babb" "abbb" "bbbb"

Upvotes: 2

Marius
Marius

Reputation: 60080

Adapting AK88's answer, outer can be used for arbitrary values of k, although it's not necessarily the most efficient solution:

input <- c('a', 'b')
k = 5
perms = input
for (i in 2:k) {
    perms = outer(perms, input, paste, sep="")
}
result = as.vector(perms)

Upvotes: 3

AK88
AK88

Reputation: 3026

m <- outer(input, input, paste, sep="")
output = as.vector(m)

## "aa" "ba" "ab" "bb"

Upvotes: 2

Related Questions