Generating all permutations of a given string

前端 未结 30 1683
我寻月下人不归
我寻月下人不归 2020-11-21 06:35

What is an elegant way to find all the permutations of a string. E.g. permutation for ba, would be ba and ab, but what about longer st

30条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-21 07:00

    Recursion is not necessary, even you can calculate any permutation directly, this solution uses generics to permute any array.

    Here is a good information about this algorihtm.

    For C# developers here is more useful implementation.

    public static void main(String[] args) {
        String word = "12345";
    
        Character[] array = ArrayUtils.toObject(word.toCharArray());
        long[] factorials = Permutation.getFactorials(array.length + 1);
    
        for (long i = 0; i < factorials[array.length]; i++) {
            Character[] permutation = Permutation.getPermutation(i, array, factorials);
            printPermutation(permutation);
        }
    }
    
    private static void printPermutation(Character[] permutation) {
        for (int i = 0; i < permutation.length; i++) {
            System.out.print(permutation[i]);
        }
        System.out.println();
    }
    

    This algorithm has O(N) time and space complexity to calculate each permutation.

    public class Permutation {
        public static  T[] getPermutation(long permutationNumber, T[] array, long[] factorials) {
            int[] sequence = generateSequence(permutationNumber, array.length - 1, factorials);
            T[] permutation = generatePermutation(array, sequence);
    
            return permutation;
        }
    
        public static  T[] generatePermutation(T[] array, int[] sequence) {
            T[] clone = array.clone();
    
            for (int i = 0; i < clone.length - 1; i++) {
                swap(clone, i, i + sequence[i]);
            }
    
            return clone;
        }
    
        private static int[] generateSequence(long permutationNumber, int size, long[] factorials) {
            int[] sequence = new int[size];
    
            for (int j = 0; j < sequence.length; j++) {
                long factorial = factorials[sequence.length - j];
                sequence[j] = (int) (permutationNumber / factorial);
                permutationNumber = (int) (permutationNumber % factorial);
            }
    
            return sequence;
        }
    
        private static  void swap(T[] array, int i, int j) {
            T t = array[i];
            array[i] = array[j];
            array[j] = t;
        }
    
        public static long[] getFactorials(int length) {
            long[] factorials = new long[length];
            long factor = 1;
    
            for (int i = 0; i < length; i++) {
                factor *= i <= 1 ? 1 : i;
                factorials[i] = factor;
            }
    
            return factorials;
        }
    }
    

提交回复
热议问题