user2142422
user2142422

Reputation: 87

String combinations with multi-character replacement

According to the entered license plate (eg. ABC123) and a list of replacement values (eg 1 replaced by I). I need to get all possibles combinations.

For example:

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

Input :

ABC123

Expected output :

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

I tried with String Combinations With Character Replacement, but I can't...

Upvotes: 0

Views: 622

Answers (2)

Ivan Stoev
Ivan Stoev

Reputation: 205769

It can easily be done by utilizing the algorithm from my answer to 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);
        }
    }
}

by adding the following method:

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

Sample usage:

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

Upvotes: 0

Arturo Menchaca
Arturo Menchaca

Reputation: 15982

You can do it using recursion, iterate for each character and call recursively using the original character and the replacement, something like this:

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

And call this methods like so:

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

Upvotes: 2

Related Questions