Generate word combination array in c#

前端 未结 3 663
闹比i
闹比i 2021-01-13 06:37

I have a string such as \"big bad dog\", how can I get an string[] array which includes all the possible word/phrase combinations?

So, I would like to return \"big\"

3条回答
  •  春和景丽
    2021-01-13 07:27

    string[] array = new string[]{"big", "bad", "dog"};
    for(ulong mask = 0; mask < (1ul << array.Length); mask++)
    {
        string permutation = "";
        for(int i = 0; i < array.Length;  i++)
        {
            if((mask & (1ul << (array.Length - 1 - i))) != 0)
            {
                permutation += array[i] + " ";
            }
        }
        Console.WriteLine(permutation);
    }
    

    EDIT: No, it can not be done using only a single regular expression.

    EDIT: Per Eric Lippert, change masks to ulong (UInt64).

提交回复
热议问题