Java permutation algorithm

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.

public static void main(String[] args) {

    String str = "ABCDEFH";

    System.out.println("Number of permutations for " + str + " is : " + factorial(str.length()));
}


public static int factorial(int n)
{
    if (n == 0)
        return 1;

    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.

Leave a Comment