There's another Stack Overflow post that was created for an algorithm related to vehicle registration numbers...
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.
The answer by @ArturoMenchaca is perfect for C#, but I'd like it in Java, but given that 'yield' is not available I really can't wrap my head around how to convert it.
How would you translate this code to Java?
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;
}
}
You would call then call the method like this..
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);