Get all possible word combinations

后端 未结 2 860
无人共我
无人共我 2020-12-06 23:02

I have a list of n words (let\'s say 26). Now I want to get a list of all possible combinations, but with a maximum of k words per row (let\'s say 5)

So when my word

相关标签:
2条回答
  • 2020-12-06 23:40

    You could take a look at this

    However, if you need to get large numbers of combinations (in the tens of millions) you should use lazy evaluation for the generation of the combinations.

    0 讨论(0)
  • 2020-12-07 00:04

    i wrote simple a function to do this

            private string allState(int index,string[] inStr)
            {
                string a = inStr[index].ToString();
                int l = index+1;
                int k = l;
                var result = string.Empty;
                var t = inStr.Length;
                int i = index;
                while (i < t)
                {
                    string s = a;
                    for (int j = l; j < k; j++)
                    {
                        s += inStr[j].ToString();
                    }
                    result += s+",";
                    k++;
                    i++;
                }
    
                index++;
                if(index<inStr.Length)
                    result += allState(index, inStr);
                return result.TrimEnd(new char[] { ',' });
            }
    
    allState(0, new string[] { "a", "b", "c"})
    
    0 讨论(0)
提交回复
热议问题