permutations/combinations
Posted: Thu Feb 05, 2004 12:23 pm
Hi all,
I'm really confused, and have been trying to figure out an algorithm to do the following:
Given an array: [a,b,c,d]
I would like to produce the following array:
[
a,
b,
c,
d,
ab,
ac,
ad,
bc,
bd,
cd,
abc,
abd,
acd,
bcd,
abcd
]
If you can't see the pattern, I'm basically taking all the combinations of the elements of the first array of size 1, size 2, ... size length of that array.
More importantly, the combinations should not repeat eachother since order does not matter. (ie. abc == cab)
The algorithm should work for any length of array.
I've been trying to figure out a recursive algorithm, then an iterative algorithm and I just can't figure it out!
If anyone can help, I'd really appreciate it.
If you have further questions or comments, please let me know. Any and all feedback will help.
Thanks,
Sadiq.
I'm really confused, and have been trying to figure out an algorithm to do the following:
Given an array: [a,b,c,d]
I would like to produce the following array:
[
a,
b,
c,
d,
ab,
ac,
ad,
bc,
bd,
cd,
abc,
abd,
acd,
bcd,
abcd
]
If you can't see the pattern, I'm basically taking all the combinations of the elements of the first array of size 1, size 2, ... size length of that array.
More importantly, the combinations should not repeat eachother since order does not matter. (ie. abc == cab)
The algorithm should work for any length of array.
I've been trying to figure out a recursive algorithm, then an iterative algorithm and I just can't figure it out!
If anyone can help, I'd really appreciate it.
If you have further questions or comments, please let me know. Any and all feedback will help.
Thanks,
Sadiq.