public static String [ ] getAllPermutations ( String [ ] alphabet , int wordLength ) { // initialize our returned list with the number of elements calculated above String [ ] allLists = new String [ ( int ) Math . pow ( alphabet . length , wordLength ) ] ; // lists of length 1 are just the original elements if ( wordLength == 1 ) return alphabet ; else { // the recursion--get all lists of length 3, length 2, all the way up to 1 String [ ] allSublists = getAllPermutations ( alphabet , wordLength - 1 ) ; // append the sublists to each element int arrayIndex = 0 ; for ( int i = 0 ; i < alphabet . length ; i ++ ) { for ( int j = 0 ; j < allSublists . length ; j ++ ) { // add the newly appended combination to the list allLists [ arrayIndex ] = alphabet [ i ] + allSublists [ j ] ; arrayIndex ++ ; } } return allLists ; } }
Get all permutations of the given alphabet of given length .