Get all possible word combinations

此生再无相见时 提交于 2019-11-28 01:37:42

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.

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"})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!