Home > Back-end >  Loop to get all probabilities of combining words
Loop to get all probabilities of combining words

Time:01-22

I have text that contains spaces. I want all the possibilities. For example, merge a space and leave another. For example, this is the first possibility: the first word, then a space, the second word, then a space, then the third word. The second possibility: the first word, then without a space, then the second word, then a space, then the third word. The third possibility: the first word, then a space, then the second word, then without a space, then the third word ... etc.. I want to do this, but in a loop, especially if the number of words is more than five, six, or more.

    static void Main(string[] args)
    {
        string test = "aaa bbb ccc";
        var ch = test.Split(' ');
        var t1 = ch[0]   " "   ch[1]   " "   ch[2];
        var t2 = ch[0]   ""   ch[1]   " "   ch[2];
        var t3 = ch[0]   " "   ch[1]   ""   ch[2];
        var t4 = ch[0]   ""   ch[1]   ""   ch[2];

        Console.WriteLine(t1);
        Console.WriteLine(t2);
        Console.WriteLine(t3);
        Console.WriteLine(t4);

        string test2 = "aaa bbb ccc ddd";
        var ch2 = test2.Split(' ');
        var z1 = ch2[0]   " "   ch2[1]   " "   ch2[2]   " "   ch2[3];
        var z2 = ch2[0]   ""   ch2[1]   " "   ch2[2]   " "   ch2[3];
        var z3 = ch2[0]   " "   ch2[1]   ""   ch2[2]   " "   ch2[3];
        var z4 = ch2[0]   " "   ch2[1]   " "   ch2[2]   ""   ch2[3];
        var z5 = ch2[0]   ""   ch2[1]   ""   ch2[2]   " "   ch2[3];
        var z6 = ch2[0]   " "   ch2[1]   ""   ch2[2]   ""   ch2[3];
        var z7 = ch2[0]   ""   ch2[1]   ""   ch2[2]   ""   ch2[3];

        Console.WriteLine(z1);
        Console.WriteLine(z2);
        Console.WriteLine(z3);
        Console.WriteLine(z4);
        Console.WriteLine(z5);
        Console.WriteLine(z6);
        Console.WriteLine(z7);

        Console.ReadLine();
    }

CodePudding user response:

So if we have n words we have n - 1 possible spaces. Let 0 be absence of space, when 1 be a presence of space:

[0, 0, 0] => Word1Word2Word3Word4
[0, 0, 1] => Word1Word2Word3 Word4
[0, 1, 0] => Word1Word2 Word3Word4
[0, 1, 1] => Word1Word2 Word3 Word4
...
[1, 1, 1] => Word1 Word2 Word3 Word4

So far so good we should enumerate all 2 ^ (n - 1) binary masks:

using System.Collections.Generic;
using System.Linq;
using System.Text;

...

private static IEnumerable<string> Solution(params string[] words) {
  return Enumerable
    .Range(0, 1 << (words.Length - 1))
    .Select(mask => {
       StringBuilder sb = new StringBuilder(words[0]);

       for (int i = 0; i < words.Length - 1;   i) {
         if ((mask & (1 << i)) != 0)
           sb.Append(' ');

         sb.Append(words[i   1]);
       }
  
       return sb.ToString();
    });
}

Demo (fiddle):

var result = string.Join(Environment.NewLine, Solution("A", "B", "C", "D"));
        
Console.WriteLine(result);

Output:

ABCD
A BCD
AB CD
A B CD
ABC D
A BC D
AB C D
A B C D

CodePudding user response:

The code you provided works for a limited number of words, but becomes impractical as the number of words increases. A more efficient way to generate all possibilities of a string containing spaces is to use loops and recursion. Here's an example of a function that takes a string and generates all possible combinations of that string (with and without spaces):

private static List<string> GenerateCombinations(string[] words, int index)
{
    if (index == words.Length)
    {
        return new List<string> { "" };
    }

    var result = new List<string>();
    var subCombinations = GenerateCombinations(words, index   1);

    foreach (var subCombination in subCombinations)
    {
        result.Add(words[index]   subCombination);
        result.Add(words[index]   " "   subCombination);
    }

    return result;
}

You can call this function with the string you want to generate combinations for:

string test = "aaa bbb ccc ddd";
var words = test.Split(' ');
var combinations = GenerateCombinations(words, 0);
foreach(var combination in combinations)
{
    Console.WriteLine(combination);
}

The function uses recursion to generate all possible combinations of strings. The index parameter is used to keep track of the current word in the word array, and the recursion stops when the index equals the length of the word array. The function uses two lists to store combinations: one for the current index and one for the next index. The current list is added to the next list in two ways, with and without spaces.

This approach will work for any number of words and will generate all possible combinations efficiently.

  • Related