I'm trying to get all the predefined-length permutations of a list, in ascending order only.
For example, take the set: "ABCDE"
I'd like the returning result to be:
ABC, ABD, ABE, ACD, ACE, ADE, BCD, BCE, BDE, CDE
In other words, a "B" can never appear before an "A" (ascending order), but I'd like every variation within this requirements.
I'd prefer not to use LINQ, and I'm trying to figure out the fastest way to implement this (speed is a factor in this app).
So far I have a list of lists of chars:
List<List<char>> Combinations;
where the inner "List" would be a combination like "ABC" (each letter being a char), and the outer list would be a list of all combinations.
The length of each resulting set (3 in the above example) needs to be dynamic, so I'm thinking I'll need some kind of recursion... I just can't figure out how to implement it.
Any help would be greatly appreciated!
EDIT
So far, here's what I have (I feel like I'm getting close... I just can't get it to actually build the final list (the union isn't working - am I using it incorrectly?):
private List<List<char>> AscendingPermute(List<char> InMovements, int Combinations)
{
List<List<char>> Ret = new List<List<char>>();
for(int i = 0; i <= InMovements.Count - Combinations; i++)
{
if(Combinations <= 1){
Ret.Add(new List<char>() {InMovements[i] });
return Ret;
}
else
{
Ret.Union(AscendingPermute(InMovements.GetRange(1, InMovements.Count - 1), Combinations - 1));
}
}
return Ret;
}
Am I on the right track? What am I missing?
Thanks!