【问题标题】:String combinations with multi-character replacement具有多字符替换的字符串组合
【发布时间】:2016-08-22 10:37:16
【问题描述】:

根据输入的车牌(例如ABC123)和替换值列表(例如1 替换为I)。我需要得到所有可能的组合。

例如:

1 => I
3 => B
A => H
0 (zero) => O
O => 0 (zero)

输入:

ABC123

预期输出:

ABC123, ABCI23, ABCI28, ABC128, HBC123, HBCI23, HBCI28, HBC128

我试过String Combinations With Character Replacement,但我不能...

【问题讨论】:

  • 发布你尝试过的东西,不管它是否有效。

标签: c# algorithm combinations


【解决方案1】:

您可以使用递归,迭代每个字符并使用原始字符和替换进行递归调用,如下所示:

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);

【讨论】:

  • 很好的答案@ArturoMenchaca - 我正在使用它,但如果我希望将“1”的所有组合调整为“I”和“L”,如何最好地实现?我确实尝试将您的代码调整为 的字典,但后来我开始怀疑这是否可能,然后我复制并粘贴了两个变体的代码,然后将结果合并到一个不同的集合中.只是想知道它应该如何完成。
  • @DevologyLtd:您可以使用 char[] 如您所说,然后在if (replacements.ContainsKey... 语句中在递归调用之前添加foreach (var replace in replacements[original[index]]),并在调用中将current + replacements[original[index]] 替换为current + replace .
  • 我无法在 Java 中做枚举器/返回产量。我提出了stackoverflow.com/questions/49371495/… 并想知道你是否可以帮助@ArturoMenchaca
【解决方案2】:

利用我对Looking at each combination in jagged array的回答中的算法可以轻松完成:

public static class Algorithms
{
    public static IEnumerable<string> GetCombinations(this char[][] input)
    {
        var result = new char[input.Length]; 
        var indices = new int[input.Length];
        for (int pos = 0, index = 0; ;)
        {
            for (; pos < input.Length; pos++, index = 0)
            {
                indices[pos] = index;
                result[pos] = input[pos][index];
            }
            yield return new string(result);
            do
            {
                if (pos == 0) yield break;
                index = indices[--pos] + 1;
            }
            while (index >= input[pos].Length);
        }
    }
}

通过添加以下方法:

public static IEnumerable<string> GetCombinations(this string input, Dictionary<char, char> replacements)
{
    return input.Select(c =>
    {
        char r;
        return replacements.TryGetValue(c, out r) && c != r ? new[] { c, r } : new[] { c };
    }).ToArray().GetCombinations();
}

示例用法:

var replacements = new Dictionary<char, char> { { '1', 'I' }, { '3', '8' }, { 'A', 'H' }, { '0', 'O' }, { 'O', '0' } };
var test = "ABC123".GetCombinations(replacements);
foreach (var comb in test) Console.WriteLine(comb);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 2019-07-31
    • 2022-08-17
    • 2020-08-18
    • 2015-03-15
    相关资源
    最近更新 更多