Generating all permutations of a certain length

后端 未结 3 664
-上瘾入骨i
-上瘾入骨i 2020-12-06 12:59

Suppose we have an alphabet \"abcdefghiklimnop\". How can I recursively generate permutations with repetition of this alphabet in groups of FIVE in an efficient way?

相关标签:
3条回答
  • 2020-12-06 13:10

    In order to pick five characters from a string recursively, follow a simple algorithm:

    • Your method should get a portion filled in so far, and the first position in the five-character permutation that needs a character
    • If the first position that needs a character is above five, you are done; print the combination that you have so far, and return
    • Otherwise, put each character into the current position in the permutation, and make a recursive call

    This is a lot shorter in Java:

    private static void permutation(char[] perm, int pos, String str) {
        if (pos == perm.length) {
            System.out.println(new String(perm));
        } else {
            for (int i = 0 ; i < str.length() ; i++) {
                perm[pos] = str.charAt(i);
                permutation(perm, pos+1, str);
            }
        }
    }
    

    The caller controls the desired length of permutation by changing the number of elements in perm:

    char[] perm = new char[5];
    permutation(perm, 0, "abcdefghiklimnop");
    

    Demo.

    0 讨论(0)
  • 2020-12-06 13:10

    This is can be easily done using bit manipulation.

    private void getPermutation(String str, int length)
            {
                if(str==null)
                    return;
                Set<String> StrList = new HashSet<String>();
                StringBuilder strB= new StringBuilder();
                for(int i = 0;i < (1 << str.length()); ++i)
                {
                    strB.setLength(0); //clear the StringBuilder
                    if(getNumberOfOnes(i)==length){
                      for(int j = 0;j < str.length() ;++j){
                        if((i & (1 << j))>0){  // to check whether jth bit is set (is 1 or not)
                            strB.append(str.charAt(j));
                        }
                    }
                    StrList.add(strB.toString());
                    }
                }
                System.out.println(Arrays.toString(StrList.toArray()));
            }
    
        private int getNumberOfOnes (int n) // to count how many numbers of 1 in binary representation of n
        {
            int count=0;
            while( n>0 )
            {
            n = n&(n-1);
               count++;
            }
            return count;
        }
    
    0 讨论(0)
  • 2020-12-06 13:14

    All permutations of five characters will be contained in the set of the first five characters of every permutation. For example, if you want all two character permutations of a four character string 'abcd' you can obtain them from all permutations: 'abcd', 'abdc', 'acbd','acdb' ... 'dcba'

    So instead of printing them in your method you can store them to a list after checking to see if that permutation is already stored. The list can either be passed in to the function or a static field, depending on your specification.

    0 讨论(0)
提交回复
热议问题