How to find permutation of k in a given length?

前端 未结 6 674
小鲜肉
小鲜肉 2020-12-05 17:01

How can I find the permutations of k in a given length?

For example:

The word cat has 3 letters: How can I find all the permutations of 2 in the

6条回答
  •  自闭症患者
    2020-12-05 17:28

    Here is one in C#, which should work even with repeated characters. For example on "banana" for permutations of length 2 it gives:

    ba bn ab aa an nb na nn

    The basic idea is to fix the first character, then form all permutations of length k-1, then prepend the character to those k-1 length permutations. To deal with duplicate characters, we keep track of the count left (i.e the ones which can be used for sub-permutations).

    Not exemplary code, but should give you the idea. (If you find bugs, let me know and I can edit).

    static List Permutations(Dictionary input, int length) {
        List permutations = new List();
    
        List chars = new List(input.Keys);
    
        // Base case.
        if (length == 0) {
            permutations.Add(string.Empty);
            return permutations;
        }
    
        foreach (char c in chars) {
    
            // There are instances of this character left to use.
            if (input[c] > 0) {
    
                // Use one instance up.
                input[c]--;
    
                // Find sub-permutations of length length -1.
                List subpermutations = Permutations(input, length - 1);
    
                // Give back the instance.
                input[c]++;
    
                foreach (string s in subpermutations) {
    
                    // Prepend the character to be the first character.
                    permutations.Add(s.Insert(0,new string(c,1)));
    
                }
            }
        }
    
        return permutations;
    }
    

    And here is the full program I have, to use it:

    using System;
    using System.Collections.Generic;
    
    namespace StackOverflow {
    
        class Program {
            static void Main(string[] args) {
                List p = Permutations("abracadabra", 3);
                foreach (string s in p) {
                    Console.WriteLine(s);
                }
            }
    
            static List Permutations(string s, int length) {
                Dictionary input = new Dictionary();
                foreach (char c in s) {
                    if (input.ContainsKey(c)) {
                        input[c]++;
                    } else {
                        input[c] = 1;
                    }
                }
                return Permutations(input, length);
            }
    
            static List Permutations(Dictionary input, 
                                                              int length) {
                List permutations = new List();
    
                List chars = new List(input.Keys);
                if (length == 0) {
                    permutations.Add(string.Empty);
                    return permutations;
                }
    
                foreach (char c in chars) {
                    if (input[c] > 0) {
                        input[c]--;
                        List subpermutations = Permutations(input, 
                                                                    length - 1);
                        input[c]++;
    
                        foreach (string s in subpermutations) {
                            permutations.Add(s.Insert(0,new string(c,1)));
                        }
                    }
                }
    
                return permutations;
            }
        }
    }
    

提交回复
热议问题