Get all possible phrases

I have a list of n words (say 26). Now I want to get a list of all possible combinations, but no more than k words per line (let's say 5)

So when my wordlist is: aaa, bbb, ..., zzz I want to get:

aaa
bbb
...
aaabbb
aaaccc
...
aaabbbcccdddeeefff
aaabbbcccdddeeeggg
...

      

I want to make it a variable so that it works with any value of n or k. Words should not be twice, and all combinations should be accepted (even if there are a lot of them).

How could I achieve this?

EDIT:

Thank you for your responses. This is not a task. I just forgot about my password combinations and I want to be sure that I have all the combinations tested. Although I don't have 26 passwords, it made it easier to explain what I want.

If there are other people with the same problem, this link might be helpful:
Create an array of word combinations in C #

+2


source to share


2 answers


You can take a look at this



However, if you need to get a large number of combinations (tens of millions), you should use lazy evaluation to generate the combinations.

+2


source


I wrote a simple function for 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"})

      

+3


source







All Articles