您可以使用遞歸做到這一點,遍歷每個性格和遞歸調用使用原有的特色和更換,這樣的事情:
public static IEnumerable<string> Combinations(string s, Dictionary<char, char> replacements)
{
return Combinations(s, replacements, 0, string.Empty);
}
private static IEnumerable<string> Combinations(string original, Dictionary<char, char> replacements, int index, string current)
{
if (index == original.Length) yield return current;
else
{
foreach (var item in Combinations(original, replacements, index + 1, current + original[index]))
yield return item;
if (replacements.ContainsKey(original[index]))
foreach (var item in Combinations(original, replacements, index + 1, current + replacements[original[index]]))
yield return item;
}
}
而且把這種方法就像這樣:
Dictionary<char, char> dict = new Dictionary<char,char>();
dict['1'] = 'I';
dict['3'] = 'B';
dict['A'] = 'H';
dict['O'] = '0';
dict['0'] = 'O';
var combs = Combinations("ABC123", dict);
發佈你已經嘗試過的,如果它工作或沒有關係,無所謂噸。 – Xiaoy312