What is the best algorithm to get all possible string combinations from a given array with a minimum & maximum length value.
Note: This adds complexity since the
Best and neat way to solve this problem is to use recursive solution, hope in-line comments are good enough to understand the solution:
<?php
// Recursive method that print all permutation of a given length
// @param $arr input array
// @param $arrLen just the length of the above array
// @param $size length or size for which we are making all permutation
// @param $perArr this is a temporary array to hold the current permutation being created
// @param $pos current position of $perArr
function printPermutation($arr, $arrLen, $size, $perArr, $pos) {
if ($size==$pos) { //if $pos reach $size then we have found one permutation
for ($i=0; $i<$size; $i++) print $perArr[$i];
print ("\n");
return;
}
for ($i=0; $i<$arrLen; $i++) {
$perArr[$pos] = $arr[$i]; //put i'th char in current position
//The recursive call that move to next position with $pos+1
printPermutation($arr, $arrLen, $size, $perArr, $pos+1);
}
}
//all printPermutation method with 1 - maxLen
function showAllPermutation($letters, $maxLen) {
$length = count($letters);
$perArr = array();
for ($i=1; $i<=$maxLen; $i++) {
printPermutation ($letters, $length, $i, $perArr, 0);
}
}
//main
$letters = array('a','b','c','1','2','3');
$max_length = 4;
showAllPermutation ($letters, $max_length);
?>
If you are interested, here is the very big output of the above code: stdout:
a
b
c
1
2
3
aa
ab
ac
a1
a2
a3
ba
bb
bc
b1
b2
b3
ca
cb
cc
c1
c2
c3
1a
1b
1c
11
12
13
2a
2b
2c
21
22
23
3a
3b
3c
31
32
33
aaa
aab
aac
aa1
aa2
aa3
aba
abb
abc
ab1
ab2
ab3
aca
acb
acc
ac1
ac2
ac3
a1a
a1b
a1c
a11
a12
a13
a2a
a2b
a2c
a21
a22
a23
a3a
a3b
a3c
a31
a32
a33
baa
bab
bac
ba1
ba2
ba3
bba
bbb
bbc
bb1
bb2
bb3
bca
bcb
bcc
bc1
bc2
bc3
b1a
b1b
b1c
b11
b12
b13
b2a
b2b
b2c
b21
b22
b23
b3a
b3b
b3c
b31
b32
b33
caa
cab
cac
ca1
ca2
ca3
cba
cbb
cbc
cb1
cb2
cb3
cca
ccb
ccc
cc1
cc2
cc3
c1a
c1b
c1c
c11
c12
c13
c2a
c2b
c2c
c21
c22
c23
c3a
c3b
c3c
c31
c32
c33
1aa
1ab
1ac
1a1
1a2
1a3
1ba
1bb
1bc
1b1
1b2
1b3
1ca
1cb
1cc
1c1
1c2
1c3
11a
11b
11c
111
112
113
12a
12b
12c
121
122
123
13a
13b
13c
131
132
133
2aa
2ab
2ac
2a1
2a2
2a3
2ba
2bb
2bc
2b1
2b2
2b3
2ca
2cb
2cc
2c1
2c2
2c3
21a
21b
21c
211
212
213
22a
22b
22c
221
222
223
23a
23b
23c
231
232
233
3aa
3ab
3ac
3a1
3a2
3a3
3ba
3bb
3bc
3b1
3b2
3b3
3ca
3cb
3cc
3c1
3c2
3c3
31a
31b
31c
311
312
313
32a
32b
32c
321
322
323
33a
33b
33c
331
332
333
aaaa
aaab
aaac
aaa1
aaa2
aaa3
aaba
aabb
aabc
aab1
aab2
aab3
aaca
aacb
aacc
aac1
aac2
aac3
aa1a
aa1b
aa1c
aa11
aa12
aa13
aa2a
aa2b
aa2c
aa21
aa22
aa23
aa3a
aa3b
aa3c
aa31
aa32
aa33
abaa
abab
abac
aba1
aba2
aba3
abba
abbb
abbc
abb1
abb2
abb3
abca
abcb
abcc
abc1
abc2
abc3
ab1a
ab1b
ab1c
ab11
ab12
ab13
ab2a
ab2b
ab2c
ab21
ab22
ab23
ab3a
ab3b
ab3c
ab31
ab32
ab33
acaa
acab
acac
aca1
aca2
aca3
acba
acbb
acbc
acb1
acb2
acb3
acca
accb
accc
acc1
acc2
acc3
ac1a
ac1b
ac1c
ac11
ac12
ac13
ac2a
ac2b
ac2c
ac21
ac22
ac23
ac3a
ac3b
ac3c
ac31
ac32
ac33
a1aa
a1ab
a1ac
a1a1
a1a2
a1a3
a1ba
a1bb
a1bc
a1b1
a1b2
a1b3
a1ca
a1cb
a1cc
a1c1
a1c2
a1c3
a11a
a11b
a11c
a111
a112
a113
a12a
a12b
a12c
a121
a122
a123
a13a
a13b
a13c
a131
a132
a133
a2aa
a2ab
a2ac
a2a1
a2a2
a2a3
a2ba
a2bb
a2bc
a2b1
a2b2
a2b3
a2ca
a2cb
a2cc
a2c1
a2c2
a2c3
a21a
a21b
a21c
a211
a212
a213
a22a
a22b
a22c
a221
a222
a223
a23a
a23b
a23c
a231
a232
a233
a3aa
a3ab
a3ac
a3a1
a3a2
a3a3
a3ba
a3bb
a3bc
a3b1
a3b2
a3b3
a3ca
a3cb
a3cc
a3c1
a3c2
a3c3
a31a
a31b
a31c
a311
a312
a313
a32a
a32b
a32c
a321
a322
a323
a33a
a33b
a33c
a331
a332
a333
baaa
baab
baac
baa1
baa2
baa3
baba
babb
babc
bab1
bab2
bab3
baca
bacb
bacc
bac1
bac2
bac3
ba1a
ba1b
ba1c
ba11
ba12
ba13
ba2a
ba2b
ba2c
ba21
ba22
ba23
ba3a
ba3b
ba3c
ba31
ba32
ba33
bbaa
bbab
bbac
bba1
bba2
bba3
bbba
bbbb
bbbc
bbb1
bbb2
bbb3
bbca
bbcb
bbcc
bbc1
bbc2
bbc3
bb1a
bb1b
bb1c
bb11
bb12
bb13
bb2a
bb2b
bb2c
bb21
bb22
bb23
bb3a
bb3b
bb3c
bb31
bb32
bb33
bcaa
bcab
bcac
bca1
bca2
bca3
bcba
bcbb
bcbc
bcb1
bcb2
bcb3
bcca
bccb
bccc
bcc1
bcc2
bcc3
bc1a
bc1b
bc1c
bc11
bc12
bc13
bc2a
bc2b
bc2c
bc21
bc22
bc23
bc3a
bc3b
bc3c
bc31
bc32
bc33
b1aa
b1ab
b1ac
b1a1
b1a2
b1a3
b1ba
b1bb
b1bc
b1b1
b1b2
b1b3
b1ca
b1cb
b1cc
b1c1
b1c2
b1c3
b11a
b11b
b11c
b111
b112
b113
b12a
b12b
b12c
b121
b122
b123
b13a
b13b
b13c
b131
b132
b133
b2aa
b2ab
b2ac
b2a1
b2a2
b2a3
b2ba
b2bb
b2bc
b2b1
b2b2
b2b3
b2ca
b2cb
b2cc
b2c1
b2c2
b2c3
b21a
b21b
b21c
b211
b212
b213
b22a
b22b
b22c
b221
b222
b223
b23a
b23b
b23c
b231
b232
b233
b3aa
b3ab
b3ac
b3a1
b3a2
b3a3
b3ba
b3bb
b3bc
b3b1
b3b2
b3b3
b3ca
b3cb
b3cc
b3c1
b3c2
b3c3
b31a
b31b
b31c
b311
b312
b313
b32a
b32b
b32c
b321
b322
b323
b33a
b33b
b33c
b331
b332
b333
caaa
caab
caac
caa1
caa2
caa3
caba
cabb
cabc
cab1
cab2
cab3
caca
cacb
cacc
cac1
cac2
cac3
ca1a
ca1b
ca1c
ca11
ca12
ca13
ca2a
ca2b
ca2c
ca21
ca22
ca23
ca3a
ca3b
ca3c
ca31
ca32
ca33
cbaa
cbab
cbac
cba1
cba2
cba3
cbba
cbbb
cbbc
cbb1
cbb2
cbb3
cbca
cbcb
cbcc
cbc1
cbc2
cbc3
cb1a
cb1b
cb1c
cb11
cb12
cb13
cb2a
cb2b
cb2c
cb21
cb22
cb23
cb3a
cb3b
cb3c
cb31
cb32
cb33
ccaa
ccab
ccac
cca1
cca2
cca3
ccba
ccbb
ccbc
ccb1
ccb2
ccb3
ccca
cccb
cccc
ccc1
ccc2
ccc3
cc1a
cc1b
cc1c
cc11
cc12
cc13
cc2a
cc2b
cc2c
cc21
cc22
cc23
cc3a
cc3b
cc3c
cc31
cc32
cc33
c1aa
c1ab
c1ac
c1a1
c1a2
c1a3
c1ba
c1bb
c1bc
c1b1
c1b2
c1b3
c1ca
c1cb
c1cc
c1c1
c1c2
c1c3
c11a
c11b
c11c
c111
c112
c113
c12a
c12b
c12c
c121
c122
c123
c13a
c13b
c13c
c131
c132
c133
c2aa
c2ab
c2ac
c2a1
c2a2
c2a3
c2ba
c2bb
c2bc
c2b1
c2b2
c2b3
c2ca
c2cb
c2cc
c2c1
c2c2
c2c3
c21a
c21b
c21c
c211
c212
c213
c22a
c22b
c22c
c221
c222
c223
c23a
c23b
c23c
c231
c232
c233
c3aa
c3ab
c3ac
c3a1
c3a2
c3a3
c3ba
c3bb
c3bc
c3b1
c3b2
c3b3
c3ca
c3cb
c3cc
c3c1
c3c2
c3c3
c31a
c31b
c31c
c311
c312
c313
c32a
c32b
c32c
c321
c322
c323
c33a
c33b
c33c
c331
c332
c333
1aaa
1aab
1aac
1aa1
1aa2
1aa3
1aba
1abb
1abc
1ab1
1ab2
1ab3
1aca
1acb
1acc
1ac1
1ac2
1ac3
1a1a
1a1b
1a1c
1a11
1a12
1a13
1a2a
1a2b
1a2c
1a21
1a22
1a23
1a3a
1a3b
1a3c
1a31
1a32
1a33
1baa
1bab
1bac
1ba1
1ba2
1ba3
1bba
1bbb
1bbc
1bb1
1bb2
1bb3
1bca
1bcb
1bcc
1bc1
1bc2
1bc3
1b1a
1b1b
1b1c
1b11
1b12
1b13
1b2a
1b2b
1b2c
1b21
1b22
1b23
1b3a
1b3b
1b3c
1b31
1b32
1b33
1caa
1cab
1cac
1ca1
1ca2
1ca3
1cba
1cbb
1cbc
1cb1
1cb2
1cb3
1cca
1ccb
1ccc
1cc1
1cc2
1cc3
1c1a
1c1b
1c1c
1c11
1c12
1c13
1c2a
1c2b
1c2c
1c21
1c22
1c23
1c3a
1c3b
1c3c
1c31
1c32
1c33
11aa
11ab
11ac
11a1
11a2
11a3
11ba
11bb
11bc
11b1
11b2
11b3
11ca
11cb
11cc
11c1
11c2
11c3
111a
111b
111c
1111
1112
1113
112a
112b
112c
1121
1122
1123
113a
113b
113c
1131
1132
1133
12aa
12ab
12ac
12a1
12a2
12a3
12ba
12bb
12bc
12b1
12b2
12b3
12ca
12cb
12cc
12c1
12c2
12c3
121a
121b
121c
1211
1212
1213
122a
122b
122c
1221
1222
1223
123a
123b
123c
1231
1232
1233
13aa
13ab
13ac
13a1
13a2
13a3
13ba
13bb
13bc
13b1
13b2
13b3
13ca
13cb
13cc
13c1
13c2
13c3
131a
131b
131c
1311
1312
1313
132a
132b
132c
1321
1322
1323
133a
133b
133c
1331
1332
1333
2aaa
2aab
2aac
2aa1
2aa2
2aa3
2aba
2abb
2abc
2ab1
2ab2
2ab3
2aca
2acb
2acc
2ac1
2ac2
2ac3
2a1a
2a1b
2a1c
2a11
2a12
2a13
2a2a
2a2b
2a2c
2a21
2a22
2a23
2a3a
2a3b
2a3c
2a31
2a32
2a33
2baa
2bab
2bac
2ba1
2ba2
2ba3
2bba
2bbb
2bbc
2bb1
2bb2
2bb3
2bca
2bcb
2bcc
2bc1
2bc2
2bc3
2b1a
2b1b
2b1c
2b11
2b12
2b13
2b2a
2b2b
2b2c
2b21
2b22
2b23
2b3a
2b3b
2b3c
2b31
2b32
2b33
2caa
2cab
2cac
2ca1
2ca2
2ca3
2cba
2cbb
2cbc
2cb1
2cb2
2cb3
2cca
2ccb
2ccc
2cc1
2cc2
2cc3
2c1a
2c1b
2c1c
2c11
2c12
2c13
2c2a
2c2b
2c2c
2c21
2c22
2c23
2c3a
2c3b
2c3c
2c31
2c32
2c33
21aa
21ab
21ac
21a1
21a2
21a3
21ba
21bb
21bc
21b1
21b2
21b3
21ca
21cb
21cc
21c1
21c2
21c3
211a
211b
211c
2111
2112
2113
212a
212b
212c
2121
2122
2123
213a
213b
213c
2131
2132
2133
22aa
22ab
22ac
22a1
22a2
22a3
22ba
22bb
22bc
22b1
22b2
22b3
22ca
22cb
22cc
22c1
22c2
22c3
221a
221b
221c
2211
2212
2213
222a
222b
222c
2221
2222
2223
223a
223b
223c
2231
2232
2233
23aa
23ab
23ac
23a1
23a2
23a3
23ba
23bb
23bc
23b1
23b2
23b3
23ca
23cb
23cc
23c1
23c2
23c3
231a
231b
231c
2311
2312
2313
232a
232b
232c
2321
2322
2323
233a
233b
233c
2331
2332
2333
3aaa
3aab
3aac
3aa1
3aa2
3aa3
3aba
3abb
3abc
3ab1
3ab2
3ab3
3aca
3acb
3acc
3ac1
3ac2
3ac3
3a1a
3a1b
3a1c
3a11
3a12
3a13
3a2a
3a2b
3a2c
3a21
3a22
3a23
3a3a
3a3b
3a3c
3a31
3a32
3a33
3baa
3bab
3bac
3ba1
3ba2
3ba3
3bba
3bbb
3bbc
3bb1
3bb2
3bb3
3bca
3bcb
3bcc
3bc1
3bc2
3bc3
3b1a
3b1b
3b1c
3b11
3b12
3b13
3b2a
3b2b
3b2c
3b21
3b22
3b23
3b3a
3b3b
3b3c
3b31
3b32
3b33
3caa
3cab
3cac
3ca1
3ca2
3ca3
3cba
3cbb
3cbc
3cb1
3cb2
3cb3
3cca
3ccb
3ccc
3cc1
3cc2
3cc3
3c1a
3c1b
3c1c
3c11
3c12
3c13
3c2a
3c2b
3c2c
3c21
3c22
3c23
3c3a
3c3b
3c3c
3c31
3c32
3c33
31aa
31ab
31ac
31a1
31a2
31a3
31ba
31bb
31bc
31b1
31b2
31b3
31ca
31cb
31cc
31c1
31c2
31c3
311a
311b
311c
3111
3112
3113
312a
312b
312c
3121
3122
3123
313a
313b
313c
3131
3132
3133
32aa
32ab
32ac
32a1
32a2
32a3
32ba
32bb
32bc
32b1
32b2
32b3
32ca
32cb
32cc
32c1
32c2
32c3
321a
321b
321c
3211
3212
3213
322a
322b
322c
3221
3222
3223
323a
323b
323c
3231
3232
3233
33aa
33ab
33ac
33a1
33a2
33a3
33ba
33bb
33bc
33b1
33b2
33b3
33ca
33cb
33cc
33c1
33c2
33c3
331a
331b
331c
3311
3312
3313
332a
332b
332c
3321
3322
3323
333a
333b
333c
3331
3332
3333
This is a different language, but is this the results that you are looking for in the end?
>>> import itertools
>>> def my_combinations(string, max_length):
for repeat in range(1, max_length + 1):
for result in itertools.product(string, repeat=repeat):
yield ''.join(result)
>>> print(*my_combinations('abc123', 4), sep='\n')
a
b
c
1
2
3
aa
ab
ac
a1
a2
a3
ba
bb
bc
b1
b2
b3
ca
cb
cc
c1
c2
c3
1a
1b
1c
11
12
13
2a
2b
2c
21
22
23
3a
3b
3c
31
32
33
aaa
aab
aac
aa1
aa2
aa3
aba
abb
abc
ab1
ab2
ab3
aca
acb
acc
ac1
ac2
ac3
a1a
a1b
a1c
a11
a12
a13
a2a
a2b
a2c
a21
a22
a23
a3a
a3b
a3c
a31
a32
a33
baa
bab
bac
ba1
ba2
ba3
bba
bbb
bbc
bb1
bb2
bb3
bca
bcb
bcc
bc1
bc2
bc3
b1a
b1b
b1c
b11
b12
b13
b2a
b2b
b2c
b21
b22
b23
b3a
b3b
b3c
b31
b32
b33
caa
cab
cac
ca1
ca2
ca3
cba
cbb
cbc
cb1
cb2
cb3
cca
ccb
ccc
cc1
cc2
cc3
c1a
c1b
c1c
c11
c12
c13
c2a
c2b
c2c
c21
c22
c23
c3a
c3b
c3c
c31
c32
c33
1aa
1ab
1ac
1a1
1a2
1a3
1ba
1bb
1bc
1b1
1b2
1b3
1ca
1cb
1cc
1c1
1c2
1c3
11a
11b
11c
111
112
113
12a
12b
12c
121
122
123
13a
13b
13c
131
132
133
2aa
2ab
2ac
2a1
2a2
2a3
2ba
2bb
2bc
2b1
2b2
2b3
2ca
2cb
2cc
2c1
2c2
2c3
21a
21b
21c
211
212
213
22a
22b
22c
221
222
223
23a
23b
23c
231
232
233
3aa
3ab
3ac
3a1
3a2
3a3
3ba
3bb
3bc
3b1
3b2
3b3
3ca
3cb
3cc
3c1
3c2
3c3
31a
31b
31c
311
312
313
32a
32b
32c
321
322
323
33a
33b
33c
331
332
333
aaaa
aaab
aaac
aaa1
aaa2
aaa3
aaba
aabb
aabc
aab1
aab2
aab3
aaca
aacb
aacc
aac1
aac2
aac3
aa1a
aa1b
aa1c
aa11
aa12
aa13
aa2a
aa2b
aa2c
aa21
aa22
aa23
aa3a
aa3b
aa3c
aa31
aa32
aa33
abaa
abab
abac
aba1
aba2
aba3
abba
abbb
abbc
abb1
abb2
abb3
abca
abcb
abcc
abc1
abc2
abc3
ab1a
ab1b
ab1c
ab11
ab12
ab13
ab2a
ab2b
ab2c
ab21
ab22
ab23
ab3a
ab3b
ab3c
ab31
ab32
ab33
acaa
acab
acac
aca1
aca2
aca3
acba
acbb
acbc
acb1
acb2
acb3
acca
accb
accc
acc1
acc2
acc3
ac1a
ac1b
ac1c
ac11
ac12
ac13
ac2a
ac2b
ac2c
ac21
ac22
ac23
ac3a
ac3b
ac3c
ac31
ac32
ac33
a1aa
a1ab
a1ac
a1a1
a1a2
a1a3
a1ba
a1bb
a1bc
a1b1
a1b2
a1b3
a1ca
a1cb
a1cc
a1c1
a1c2
a1c3
a11a
a11b
a11c
a111
a112
a113
a12a
a12b
a12c
a121
a122
a123
a13a
a13b
a13c
a131
a132
a133
a2aa
a2ab
a2ac
a2a1
a2a2
a2a3
a2ba
a2bb
a2bc
a2b1
a2b2
a2b3
a2ca
a2cb
a2cc
a2c1
a2c2
a2c3
a21a
a21b
a21c
a211
a212
a213
a22a
a22b
a22c
a221
a222
a223
a23a
a23b
a23c
a231
a232
a233
a3aa
a3ab
a3ac
a3a1
a3a2
a3a3
a3ba
a3bb
a3bc
a3b1
a3b2
a3b3
a3ca
a3cb
a3cc
a3c1
a3c2
a3c3
a31a
a31b
a31c
a311
a312
a313
a32a
a32b
a32c
a321
a322
a323
a33a
a33b
a33c
a331
a332
a333
baaa
baab
baac
baa1
baa2
baa3
baba
babb
babc
bab1
bab2
bab3
baca
bacb
bacc
bac1
bac2
bac3
ba1a
ba1b
ba1c
ba11
ba12
ba13
ba2a
ba2b
ba2c
ba21
ba22
ba23
ba3a
ba3b
ba3c
ba31
ba32
ba33
bbaa
bbab
bbac
bba1
bba2
bba3
bbba
bbbb
bbbc
bbb1
bbb2
bbb3
bbca
bbcb
bbcc
bbc1
bbc2
bbc3
bb1a
bb1b
bb1c
bb11
bb12
bb13
bb2a
bb2b
bb2c
bb21
bb22
bb23
bb3a
bb3b
bb3c
bb31
bb32
bb33
bcaa
bcab
bcac
bca1
bca2
bca3
bcba
bcbb
bcbc
bcb1
bcb2
bcb3
bcca
bccb
bccc
bcc1
bcc2
bcc3
bc1a
bc1b
bc1c
bc11
bc12
bc13
bc2a
bc2b
bc2c
bc21
bc22
bc23
bc3a
bc3b
bc3c
bc31
bc32
bc33
b1aa
b1ab
b1ac
b1a1
b1a2
b1a3
b1ba
b1bb
b1bc
b1b1
b1b2
b1b3
b1ca
b1cb
b1cc
b1c1
b1c2
b1c3
b11a
b11b
b11c
b111
b112
b113
b12a
b12b
b12c
b121
b122
b123
b13a
b13b
b13c
b131
b132
b133
b2aa
b2ab
b2ac
b2a1
b2a2
b2a3
b2ba
b2bb
b2bc
b2b1
b2b2
b2b3
b2ca
b2cb
b2cc
b2c1
b2c2
b2c3
b21a
b21b
b21c
b211
b212
b213
b22a
b22b
b22c
b221
b222
b223
b23a
b23b
b23c
b231
b232
b233
b3aa
b3ab
b3ac
b3a1
b3a2
b3a3
b3ba
b3bb
b3bc
b3b1
b3b2
b3b3
b3ca
b3cb
b3cc
b3c1
b3c2
b3c3
b31a
b31b
b31c
b311
b312
b313
b32a
b32b
b32c
b321
b322
b323
b33a
b33b
b33c
b331
b332
b333
caaa
caab
caac
caa1
caa2
caa3
caba
cabb
cabc
cab1
cab2
cab3
caca
cacb
cacc
cac1
cac2
cac3
ca1a
ca1b
ca1c
ca11
ca12
ca13
ca2a
ca2b
ca2c
ca21
ca22
ca23
ca3a
ca3b
ca3c
ca31
ca32
ca33
cbaa
cbab
cbac
cba1
cba2
cba3
cbba
cbbb
cbbc
cbb1
cbb2
cbb3
cbca
cbcb
cbcc
cbc1
cbc2
cbc3
cb1a
cb1b
cb1c
cb11
cb12
cb13
cb2a
cb2b
cb2c
cb21
cb22
cb23
cb3a
cb3b
cb3c
cb31
cb32
cb33
ccaa
ccab
ccac
cca1
cca2
cca3
ccba
ccbb
ccbc
ccb1
ccb2
ccb3
ccca
cccb
cccc
ccc1
ccc2
ccc3
cc1a
cc1b
cc1c
cc11
cc12
cc13
cc2a
cc2b
cc2c
cc21
cc22
cc23
cc3a
cc3b
cc3c
cc31
cc32
cc33
c1aa
c1ab
c1ac
c1a1
c1a2
c1a3
c1ba
c1bb
c1bc
c1b1
c1b2
c1b3
c1ca
c1cb
c1cc
c1c1
c1c2
c1c3
c11a
c11b
c11c
c111
c112
c113
c12a
c12b
c12c
c121
c122
c123
c13a
c13b
c13c
c131
c132
c133
c2aa
c2ab
c2ac
c2a1
c2a2
c2a3
c2ba
c2bb
c2bc
c2b1
c2b2
c2b3
c2ca
c2cb
c2cc
c2c1
c2c2
c2c3
c21a
c21b
c21c
c211
c212
c213
c22a
c22b
c22c
c221
c222
c223
c23a
c23b
c23c
c231
c232
c233
c3aa
c3ab
c3ac
c3a1
c3a2
c3a3
c3ba
c3bb
c3bc
c3b1
c3b2
c3b3
c3ca
c3cb
c3cc
c3c1
c3c2
c3c3
c31a
c31b
c31c
c311
c312
c313
c32a
c32b
c32c
c321
c322
c323
c33a
c33b
c33c
c331
c332
c333
1aaa
1aab
1aac
1aa1
1aa2
1aa3
1aba
1abb
1abc
1ab1
1ab2
1ab3
1aca
1acb
1acc
1ac1
1ac2
1ac3
1a1a
1a1b
1a1c
1a11
1a12
1a13
1a2a
1a2b
1a2c
1a21
1a22
1a23
1a3a
1a3b
1a3c
1a31
1a32
1a33
1baa
1bab
1bac
1ba1
1ba2
1ba3
1bba
1bbb
1bbc
1bb1
1bb2
1bb3
1bca
1bcb
1bcc
1bc1
1bc2
1bc3
1b1a
1b1b
1b1c
1b11
1b12
1b13
1b2a
1b2b
1b2c
1b21
1b22
1b23
1b3a
1b3b
1b3c
1b31
1b32
1b33
1caa
1cab
1cac
1ca1
1ca2
1ca3
1cba
1cbb
1cbc
1cb1
1cb2
1cb3
1cca
1ccb
1ccc
1cc1
1cc2
1cc3
1c1a
1c1b
1c1c
1c11
1c12
1c13
1c2a
1c2b
1c2c
1c21
1c22
1c23
1c3a
1c3b
1c3c
1c31
1c32
1c33
11aa
11ab
11ac
11a1
11a2
11a3
11ba
11bb
11bc
11b1
11b2
11b3
11ca
11cb
11cc
11c1
11c2
11c3
111a
111b
111c
1111
1112
1113
112a
112b
112c
1121
1122
1123
113a
113b
113c
1131
1132
1133
12aa
12ab
12ac
12a1
12a2
12a3
12ba
12bb
12bc
12b1
12b2
12b3
12ca
12cb
12cc
12c1
12c2
12c3
121a
121b
121c
1211
1212
1213
122a
122b
122c
1221
1222
1223
123a
123b
123c
1231
1232
1233
13aa
13ab
13ac
13a1
13a2
13a3
13ba
13bb
13bc
13b1
13b2
13b3
13ca
13cb
13cc
13c1
13c2
13c3
131a
131b
131c
1311
1312
1313
132a
132b
132c
1321
1322
1323
133a
133b
133c
1331
1332
1333
2aaa
2aab
2aac
2aa1
2aa2
2aa3
2aba
2abb
2abc
2ab1
2ab2
2ab3
2aca
2acb
2acc
2ac1
2ac2
2ac3
2a1a
2a1b
2a1c
2a11
2a12
2a13
2a2a
2a2b
2a2c
2a21
2a22
2a23
2a3a
2a3b
2a3c
2a31
2a32
2a33
2baa
2bab
2bac
2ba1
2ba2
2ba3
2bba
2bbb
2bbc
2bb1
2bb2
2bb3
2bca
2bcb
2bcc
2bc1
2bc2
2bc3
2b1a
2b1b
2b1c
2b11
2b12
2b13
2b2a
2b2b
2b2c
2b21
2b22
2b23
2b3a
2b3b
2b3c
2b31
2b32
2b33
2caa
2cab
2cac
2ca1
2ca2
2ca3
2cba
2cbb
2cbc
2cb1
2cb2
2cb3
2cca
2ccb
2ccc
2cc1
2cc2
2cc3
2c1a
2c1b
2c1c
2c11
2c12
2c13
2c2a
2c2b
2c2c
2c21
2c22
2c23
2c3a
2c3b
2c3c
2c31
2c32
2c33
21aa
21ab
21ac
21a1
21a2
21a3
21ba
21bb
21bc
21b1
21b2
21b3
21ca
21cb
21cc
21c1
21c2
21c3
211a
211b
211c
2111
2112
2113
212a
212b
212c
2121
2122
2123
213a
213b
213c
2131
2132
2133
22aa
22ab
22ac
22a1
22a2
22a3
22ba
22bb
22bc
22b1
22b2
22b3
22ca
22cb
22cc
22c1
22c2
22c3
221a
221b
221c
2211
2212
2213
222a
222b
222c
2221
2222
2223
223a
223b
223c
2231
2232
2233
23aa
23ab
23ac
23a1
23a2
23a3
23ba
23bb
23bc
23b1
23b2
23b3
23ca
23cb
23cc
23c1
23c2
23c3
231a
231b
231c
2311
2312
2313
232a
232b
232c
2321
2322
2323
233a
233b
233c
2331
2332
2333
3aaa
3aab
3aac
3aa1
3aa2
3aa3
3aba
3abb
3abc
3ab1
3ab2
3ab3
3aca
3acb
3acc
3ac1
3ac2
3ac3
3a1a
3a1b
3a1c
3a11
3a12
3a13
3a2a
3a2b
3a2c
3a21
3a22
3a23
3a3a
3a3b
3a3c
3a31
3a32
3a33
3baa
3bab
3bac
3ba1
3ba2
3ba3
3bba
3bbb
3bbc
3bb1
3bb2
3bb3
3bca
3bcb
3bcc
3bc1
3bc2
3bc3
3b1a
3b1b
3b1c
3b11
3b12
3b13
3b2a
3b2b
3b2c
3b21
3b22
3b23
3b3a
3b3b
3b3c
3b31
3b32
3b33
3caa
3cab
3cac
3ca1
3ca2
3ca3
3cba
3cbb
3cbc
3cb1
3cb2
3cb3
3cca
3ccb
3ccc
3cc1
3cc2
3cc3
3c1a
3c1b
3c1c
3c11
3c12
3c13
3c2a
3c2b
3c2c
3c21
3c22
3c23
3c3a
3c3b
3c3c
3c31
3c32
3c33
31aa
31ab
31ac
31a1
31a2
31a3
31ba
31bb
31bc
31b1
31b2
31b3
31ca
31cb
31cc
31c1
31c2
31c3
311a
311b
311c
3111
3112
3113
312a
312b
312c
3121
3122
3123
313a
313b
313c
3131
3132
3133
32aa
32ab
32ac
32a1
32a2
32a3
32ba
32bb
32bc
32b1
32b2
32b3
32ca
32cb
32cc
32c1
32c2
32c3
321a
321b
321c
3211
3212
3213
322a
322b
322c
3221
3222
3223
323a
323b
323c
3231
3232
3233
33aa
33ab
33ac
33a1
33a2
33a3
33ba
33bb
33bc
33b1
33b2
33b3
33ca
33cb
33cc
33c1
33c2
33c3
331a
331b
331c
3311
3312
3313
332a
332b
332c
3321
3322
3323
333a
333b
333c
3331
3332
3333
>>>
Accumulative set product for k times with a set S which is empty initially will produce all permutations up to k-long, with n = |S| ..
Time: O(kn^2)
Space: O(kn^2)
// Enumerate all permutations, PHP
// by Khaled A Khunaifer, 25/3/2013
function setProduct($a, $b)
{
$arr = array();
foreach ($a as $s)
{
foreach ($b as $t)
{
$arr[] = $s . $t;
}
}
return $arr;
}
function enumerate ($arrary, $maxLen)
{
$enumeration = array();
for (var $i = 1; $i <= $maxLen; $i++)
{
$enumeration += setProduct($enumeration, $array);
}
}
One thing that is for sure is that what ever the algo use for this , the best time complexity for generation of all the numbers is around O((n^m+1)/(n-1)) (Assuming that you are not using any paralleled programming . ) . (Where n is size of array , m is max length .)
For which any of the above stated algo suits better .
Solution
function combinationsByLenth($arr, $len) {
$combinations = [];
$select = array_fill(0, $len, 0);
$selectCount = count($select);
$arrCount = count($arr);
$possibleCombinations = pow($arrCount, $selectCount);
while ($possibleCombinations-- > 0) {
$combination = '';
foreach ($select as $index) {
$combination .= $arr[$index];
}
$combinations[] = $combination;
for ($i = $selectCount - 1; $i >= 0; $i--) {
if ($select[$i] !== ($arrCount - 1)) {
$select[$i]++;
break;
} else {
$select[$i] = 0;
}
}
}
return $combinations;
}
function combinationsByMinMax($arr, $min, $max) {
$combinations = [];
for ($i = $min; $i <= $max; $i++) {
$combinations = array_merge($combinations, combinationsByLenth($arr, $i));
}
return $combinations;
}
print_r(combinationsByMinMax($arr, 1, 5));
Output
Array
(
[0] => a
[1] => b
[2] => c
[3] => 1
[4] => 2
[5] => 3
[6] => aa
[7] => ab
[8] => ac
[9] => a1
...
[9320] => 3332c
[9321] => 33321
[9322] => 33322
[9323] => 33323
[9324] => 3333a
[9325] => 3333b
[9326] => 3333c
[9327] => 33331
[9328] => 33332
[9329] => 33333
)
Rationale
How I Developed This Solution
I started out with a function that handled the specific case of combinations of length 5, refactored to generalize the algorithm, refactored to generalize the algorithm, etc. I've put up a quick gist so you can see the iterations to get to this working version as a case study of how to approach building this type of algorithm:
https://gist.github.com/AdamJonR/5278480
I'd go with recursive solution.
"Guess" what is the first element, and recurse on the following elements.
Repeat for all possible guesses.
pseudo code:
findCombinations(array, candidate, length):
//base clause:
if (candidate.length == length):
return
for each i from 0to array.length:
//i is the next char to add
candidate.append(array[i])
//print the candidate, since it is a unique permutation smaller then length:
print candidate
//recursively find permutations for the remianing elements
findCombinations(array,candidate,length)
//clean up
candidate.removeLasElement()
Java Code:
private static void findCombinations(char[] array, StringBuilder candidate, int length) {
//base clause:
if (candidate.length() == length)
return;
for (int i = 0; i < array.length; i++) {
//i is the next char to add
candidate.append(array[i]);
//print the candidate, since it is a unique permutation smaller then length:
System.out.println(candidate);
//recursively find permutations for the remianing elements
findCombinations(array,candidate,length);
//clean up
candidate.deleteCharAt(candidate.length()-1);
}
}
public static void findCombinations(char[] array,int length) {
findCombinations(array, new StringBuilder(), length);
}
Invoking:
char[] arr = "abcde".toCharArray();
findCombinations(arr, 3);
results in:
a
aa
aaa
aab
aac
aad
aae
ab
aba
abb
abc
abd
abe
ac
aca
acb
acc
acd
ace
ad
ada
adb
adc
add
ade
ae
aea
aeb
aec
aed
aee
b
ba
baa
bab
bac
bad
bae
bb
bba
bbb
bbc
bbd
bbe
bc
bca
bcb
bcc
bcd
bce
bd
bda
bdb
bdc
bdd
bde
be
bea
beb
bec
bed
bee
c
ca
caa
cab
cac
cad
cae
cb
cba
cbb
cbc
cbd
cbe
cc
cca
ccb
ccc
ccd
cce
cd
cda
cdb
cdc
cdd
cde
ce
cea
ceb
cec
ced
cee
d
da
daa
dab
dac
dad
dae
db
dba
dbb
dbc
dbd
dbe
dc
dca
dcb
dcc
dcd
dce
dd
dda
ddb
ddc
ffffd
dde
de
dea
deb
dec
ded
dee
e
ea
eaa
eab
eac
ead
eae
eb
eba
ebb
ebc
ebd
ebe
ec
eca
ecb
ecc
ecd
ece
ed
eda
edb
edc
edd
ede
ee
eea
eeb
eec
eed
eee