我有一组字符串,我想找到所有可能的字符串组合并将它们添加到列表中.我想最终得到一个列表,列出每个字符串组合,减去空集.
我已经创建了一个解决方案,它完全使用嵌套的for循环.但是我想更优雅地做到这一点,最好是使用LINQ,而我对它并不是那么精通,因为我还是很陌生.
解决方案应该有2 ^ n – 1个组合列表,其中n是原始集合的基数.以下是我正在寻找的正确例子:
- set = {a,b,c}
- completedListOfCombinations =
- {
- {a},{b},{a,b},{c},c},{b,c}
- }
这是我的工作,基本但丑陋的解决方案,我在https://stackoverflow.com/a/3319652/3371287的帮助下制作
- List<string> myStrings = new List<string> { "a","b","c" };
- var allCombos = new List<List<string>>();
- for (int i = 0; i < myStrings.Count; i++)
- {
- int subsetCount = allCombos.Count;
- var m = new List<string>();
- m.Add(myStrings[i]);
- allCombos.Add(m);
- for (int j = 0; j < subsetCount; j++)
- {
- string[] subset = new string[allCombos.ElementAt(j).Count + 1];
- allCombos[j].CopyTo(subset,0);
- subset[subset.Length - 1] = myStrings[i];
- allCombos.Add(subset.ToList());
- }
- }
有人能告诉我一个更优雅的解决方案吗?我已经看到了类似的LINQ解决方案,它们创建了具有阈值的笛卡尔对和列表,但我无法将它们调整到我需要的位置.
解决方法
假设列表中的所有值都是唯一的:
- List <String> list = new List<String> { "a","c" };
- var result = Enumerable
- .Range(1,(1 << list.Count) - 1)
- .Select(index => list.Where((item,idx) => ((1 << idx) & index) != 0).ToList());
要打印出来:
- Console.WriteLine(String
- .Join(Environment.NewLine,result
- .Select(line => String.Join(",",line))));
结果是
- a
- b
- a,b
- c
- a,c
- b,c
- a,c