Generate list of all possible permutations of a string

后端 未结 30 2881
故里飘歌
故里飘歌 2020-11-22 15:10

How would I go about generating a list of all possible permutations of a string between x and y characters in length, containing a variable list of characters.

Any l

30条回答
  •  一向
    一向 (楼主)
    2020-11-22 15:29

    Here is a simple word C# recursive solution:

    Method:

    public ArrayList CalculateWordPermutations(string[] letters, ArrayList words, int index)
            {
                bool finished = true;
                ArrayList newWords = new ArrayList();
                if (words.Count == 0)
                {
                    foreach (string letter in letters)
                    {
                        words.Add(letter);
                    }
                }
    
                for(int j=index; j

    Calling:

    string[] letters = new string[]{"a","b","c"};
    ArrayList words = CalculateWordPermutations(letters, new ArrayList(), 0);
    

提交回复
热议问题