列出不同字符组合的排列
Listing permutations of different combination of characters
我找到的最接近的 SO 主题在这里:Listing all permutations of a string/integer
但是我如何将它用于字符串中每个位置的不同字符集?
举个例子:我指定字符串长度为“3”。前两个位置应为 "a" 或 "b",但最后一个位置应为“1”或“2”,例如:
aa1
ba1
ab1
bb1
aa2
ab2
ba2
bb2
如果长度是固定的,您可以使用这个简单的查询来创建 cartesian product:
string chars = "ab";
int[] digits = { 1, 2 };
var query = from c1 in chars
from c2 in chars
from d1 in digits
select string.Format("{0}{1}{2}", c1, c2, d1);
string[] possibleCombinations = query.ToArray();
结果:
aa1
aa2
ab1
ab2
ba1
ba2
bb1
bb2
编辑:对于它的价值,按要求使用 lambda(查询语法更具可读性):
possibleCombinations = chars
.SelectMany(c1 => chars
.SelectMany(c2 => digits
.Select(d1 => string.Format("{0}{1}{2}", c1, c2, d1))))
.ToArray();
如果您需要一种处理动态长度的方法,您可以看看这个:
Dynamic Generation of All Possible Combinations of Index of an Array
使用此代码:
public static List<string> GenerateCombinations(char[][] characters)
{
var combinations = new List<string>();
GenerateCombinations(0, characters, new char[characters.GetLength(0)], combinations);
return combinations;
}
private static void GenerateCombinations(int level, char[][] characters, char[] current, List<string> combinations)
{
if (level == characters.GetLength(0))
{
combinations.Add(new string(current));
return;
}
foreach (var character in characters[level])
{
current[level] = character;
GenerateCombinations(level + 1, characters, current, combinations);
}
}
使用示例:
public static void Main()
{
var characters = new[]
{
new[] { 'a', 'b' },
new[] { 'a', 'b' },
new[] { '1', '2' }
};
var combinations = GenerateCombinations(characters);
foreach (var combination in combinations)
{
Console.WriteLine(combination);
}
}
输出:
aa1
aa2
ab1
ab2
ba1
ba2
bb1
bb2
我找到的最接近的 SO 主题在这里:Listing all permutations of a string/integer
但是我如何将它用于字符串中每个位置的不同字符集?
举个例子:我指定字符串长度为“3”。前两个位置应为 "a" 或 "b",但最后一个位置应为“1”或“2”,例如:
aa1
ba1
ab1
bb1
aa2
ab2
ba2
bb2
如果长度是固定的,您可以使用这个简单的查询来创建 cartesian product:
string chars = "ab";
int[] digits = { 1, 2 };
var query = from c1 in chars
from c2 in chars
from d1 in digits
select string.Format("{0}{1}{2}", c1, c2, d1);
string[] possibleCombinations = query.ToArray();
结果:
aa1
aa2
ab1
ab2
ba1
ba2
bb1
bb2
编辑:对于它的价值,按要求使用 lambda(查询语法更具可读性):
possibleCombinations = chars
.SelectMany(c1 => chars
.SelectMany(c2 => digits
.Select(d1 => string.Format("{0}{1}{2}", c1, c2, d1))))
.ToArray();
如果您需要一种处理动态长度的方法,您可以看看这个:
Dynamic Generation of All Possible Combinations of Index of an Array
使用此代码:
public static List<string> GenerateCombinations(char[][] characters)
{
var combinations = new List<string>();
GenerateCombinations(0, characters, new char[characters.GetLength(0)], combinations);
return combinations;
}
private static void GenerateCombinations(int level, char[][] characters, char[] current, List<string> combinations)
{
if (level == characters.GetLength(0))
{
combinations.Add(new string(current));
return;
}
foreach (var character in characters[level])
{
current[level] = character;
GenerateCombinations(level + 1, characters, current, combinations);
}
}
使用示例:
public static void Main()
{
var characters = new[]
{
new[] { 'a', 'b' },
new[] { 'a', 'b' },
new[] { '1', '2' }
};
var combinations = GenerateCombinations(characters);
foreach (var combination in combinations)
{
Console.WriteLine(combination);
}
}
输出:
aa1
aa2
ab1
ab2
ba1
ba2
bb1
bb2