Every permutation of the alphabet up to 29 characters?

后端 未结 13 1307
攒了一身酷
攒了一身酷 2021-02-11 01:23

I\'m attempting to write a program that will generate a text file with every possible permutation of the alphabet from one character up to twenty-nine characters. I\'ve chosen 2

13条回答
  •  我在风中等你
    2021-02-11 02:20

    A Java solution that should do the trick:

    public void characterPermutations(int length, LinkedList permutations) {
        if(length > 1) {
            characterPermutations(length - 1, permutations);
    
            ListIterator iterator = permutations.listIterator();
            while(iterator.hasNext()) {
                String permutation = iterator.next();
                for(char c = 'a'; c <= 'z'; c++) {
                    iterator.add(c + permutation);
                }
            }
    
        } else {
            for(char c = 'a'; c <= 'z'; c++) {
                permutations.add(c + "");
            }
        }
    }
    

提交回复
热议问题