I forget which is combinations and which is permutations but I think (and correct me if I'm wrong), that permutations are all the different ways we can create a string of n characters from a string of m (>n) characters. The difficulty is if the characters are not unique (because CC and CC are indistinguishab le) - Assuming, then (because it's easier) that you have a string of unique characters the method would be something like (There may be some bugs uin my thinking, but at least this should give some idea):
Code:
{
Get String from user.
Get permutation length from user:
Check perm length > String length;
startPermutations("",input, length);
}
startPermutations(String, soFar, String input, int length)
{
if (length=0)
{
print soFar;
}
else
{
for(number of character in the String)
{
soFar=soFar+currentChar;
input=input.subString(1, input.length);
startPermutations(soFar, input, length -1);
move currentChar to back of String, so we can repeat with the next;
}
}
}
Comment