我已经查看了有关组合和排列的所有 StackOverflow 帖子,但找不到我正在寻找的确切细致入微的答案,我也无法思考如何正确实现这一点。理想情况下,我正在寻找一种不使用递归的解决方案,这样我就可以避免大列表上的堆栈溢出。如何获得具有以下要求的字符串列表的所有组合?
- 每个结果列表中至少有 2 个元素
- 秩序对于平等并不重要。换句话说,("a", "b") == ("b", "a")
例子:
var input = new List<string> { "a", "b", "c", "d" };
// TODO
// Note: the final order of lists in expectedResult doesn't matter
var expectedResult = new List<List<string>>{
new List<string> { "a", "b", "c", "d" },
new List<string> { "a", "b", "c" },
new List<string> { "a", "b", "d" },
new List<string> { "a", "c", "d" },
new List<string> { "b", "c", "d" },
new List<string> { "a", "b" },
new List<string> { "a", "c", },
new List<string> { "a", "d" },
new List<string> { "b", "c" },
new List<string> { "b", "d" },
new List<string> { "c", "d" }
};
Assert.IsTrue(expectedResult.Count == 11);
Assert.IsTrue(expectedResult.All(list => list.Count >= 2));