Algorithm to generate n combinations of size k characters


alex_and_ra

I need an algorithm that generates all combinations of size n of k characters.

For example, if I have n=1 and k={a,b}, the result should be:

a
b

If n=3 and k={a,b}, the result should be:

a a a
a a b
a b a
a b b
b a a
b a b
b b a
b b b

Can someone suggest an algorithm to achieve this?

Thanks!

Mr Smith 42

You can simply map solutions to values ​​from 0 to (|k|^n)-1. The solution is just a base |k| number representation.

For example k = {a, b, c} n = 2
the solution is 0, 1, 2, ... 3^2 -1 = 8

decimal |  representation in base 3
--------+---------------------------
0       |   00
1       |   01
2       |   02
3       |   10
4       |   11
5       |   12
6       |   20
7       |   21
8       |   22

Now replace "0" with "a", "1" with "b" and "2" with "c" and you will get

aa
ab
ac
ba
bb
bc
ca
cb
cc

Related


Algorithm to generate n combinations of size k characters

alex_and_ra I need an algorithm that generates all combinations of size n of k characters. For example, if I have n=1 and k={a,b}, the result should be: a b If n=3 and k={a,b}, the result should be: a a a a a b a b a a b b b a a b a b b b a b b b Can someone

all possible combinations of k with lists of size n

Theodore Narliyski: I want to get all possible combinations of size K from a list of size N. I have a list with "person" objects and I am trying to create a new ArrayList which will be filled with the list of objects. Each table will be a different combination

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

all possible combinations of k with lists of size n

Theodore Narliyski: I want to get all possible combinations of size K from a list of size N. I have a list with "person" objects and I am trying to create a new ArrayList which will be filled with the list of objects. Each table will be a different combination

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

Get all n-size combinations with k-size letters

Scion 4581 Can someone help me? I am trying to find the formula and write a piece of code in PHP language which makes the next step Imagine we have 3 types of things, k = 1, 2, 3, and the length of this number can be different (n length), but adjacent types sh

Algorithm to return all combinations of k elements from n

Frederick I want to write a function that takes an array of letters as a parameter and selects multiple letters. Suppose you provide an array of 8 letters and want to select 3 letters from it. Then you will get: 8! / ((8 - 3)! * 3!) = 56 Returns an array (or

Algorithm to return all combinations of k elements from n

Frederick I want to write a function that takes an array of letters as a parameter and selects multiple letters. Suppose you provide an array of 8 letters and want to select 3 letters from it. Then you will get: 8! / ((8 - 3)! * 3!) = 56 Returns an array (or

Algorithm (Java) to get all combinations of size n from an array?

Esostack Right now I'm trying to write a function that takes an array and an integer n and gives a list of each combination of size n (i.e. a list of int arrays). I could write it using n nested loops, but that only works for a subset of a certain size. I don'

Algorithm (Java) to get all combinations of size n from an array?

Esostack Right now, I'm trying to write a function that takes an array and an integer n, and gives a list of each combination of size n (hence a list of int arrays). I could write it using n nested loops, but that only works for a subset of a certain size. I c

Algorithm (Java) to get all combinations of size n from an array?

Esostack Right now, I'm trying to write a function that takes an array and an integer n, and gives a list of each combination of size n (hence a list of int arrays). I could write it using n nested loops, but that only works for a subset of a certain size. I c