简体   繁体   中英

C# How to manipulate a list to build words from different chars?

I'm writing a program that takes several letters, builds words from them and return words that make sense that can be found in a word bank (like the english dictionary).

First i started with two for loops that take a letter and add to it all the other letters in ascending order: If i give (A, D, B, H) the output would be: AD, ADB, ADBH, DA, DAB, DABH etc.

var scrambledWords = new List<string>();

for (int i = 0; i < buildingMaterial.Count; i++)
{
    firstBuildUp = buildingMaterial[i];

    for (int j = 1; j < buildingMaterial.Count; j++)
    {
        if (buildingMaterial[j] == buildingMaterial[i])
            continue;

        firstBuildUp += buildingMaterial[j];
        scrambledWords.Add(firstBuildUp);
    }
}

Now i'm trying to figure out what is the best way to get all the other combinations of those letters.

  • Is it with more for loops but with different conditions in the condition parentheses?
  • Is it by creating new list for each letter and manipulate each list individually and then combining them together?
  • Is it by using Linq?

I am trying with more for loops with different conditions but wondering if its the best way achieving my goal.

What you are looking for is all the possible subsets of a set, or better said, the PowerSet of a set. Yo can have an extensions method to find power sets:

public static class ListExtensions
{
    public static List<List<T>> PowerSet<T>(this List<T> set)
    {
        var n = set.Count;

        var powerSetCount = 1 << n;

        var result = new List<List<T>>();

        for (var setMask = 0; setMask < powerSetCount; setMask++)
        {
            var subset = new List<T>();
            for (var i = 0; i < n; i++)
            {
                if ((setMask & (1 << i)) > 0)
                {
                    subset.Add(set[i]);
                }
            }

            result.Add(subset);
        }

        return result;
    }
}

And then use it like this

static class Program
{
    static void Main()
    {
        var powerSet = "abc".ToList().PowerSet();

        foreach (var set in powerSet)
        {
            // set will be a list of chars, which is equivalent to a string
            Console.WriteLine($"{new string(set.ToArray())}");
        }

        Console.ReadLine();
    }
}

The output will be:

a
b
ab
c
ac
bc
abc

Note that the empty set is also part of the PowerSet

Edit:

The string extension version:

public static class StringExtensions
{
    public static List<string> PowerSet(this string str)
    {
        var n = str.Length;

        var powerSetCount = 1 << n;

        var result = new List<string>();

        for (var setMask = 0; setMask < powerSetCount; setMask++)
        {
            var subset = new StringBuilder();
            for (var i = 0; i < n; i++)
            {
                if ((setMask & (1 << i)) > 0)
                {
                    subset.Append(str[i]);
                }
            }

            result.Add(subset.ToString());
        }

        return result;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM