Java Permutation Algorithm
So I have this code now, and in input I have in ascending order my name's letters 'ahimrsu'. I need to show up the right number for 'mariush' from all combinations which should to
Solution 1:
Your question is of mathematics nature - combinations and permutations. You are actually asking the number of possible permutation for string length 7. The formula is factorial(numOfchar).
In this case 7! = 7x6x5x4x3x2x1 = 5040.
publicstaticvoidmain(String[] args) {
String str = "ABCDEFH";
System.out.println("Number of permutations for " + str + " is : " + factorial(str.length()));
}
publicstaticintfactorial(int n)
{
if (n == 0)
return1;
int result = factorial(n-1)*n;
return result;
}
Program Output:
Number of permutations for ABCDEFH is : 5040
Since you tagged Java
, this is one way you can get the it done with Java.
Post a Comment for "Java Permutation Algorithm"