Dictionary> keyPad = new Dictionary>
{
{'2', new List{ 'A', 'B', 'C' }},
{'3', new List{ 'D', 'E', 'F' }},
{'4', new List{ 'G', 'H', 'I' }},
{'5', new List{ 'J', 'K', 'L' }},
{'6', new List{ 'M', 'N', 'O' }},
{'7', new List{ 'P', 'Q', 'R', 'S' }},
{'8', new List{ 'T', 'U', 'V' }},
{'9', new List{ 'W', 'X', 'Y', 'Z' }}
};
static void Main()
{
PrintWords("02356894");
}
void PrintWords(string number)
{
var wordBuffer = new char[number.Length];
DoPrint(wordBuffer, number, 0, 0);
}
void DoPrint(char[] wordBuffer, string number, int currentDigitIndex, int writeIndex)
{
if (currentDigitIndex == number.Length)
{
Console.WriteLine(new string(wordBuffer));
return;
}
if (number[currentDigitIndex] == '0' || number[currentDigitIndex] == '1')
{
wordBuffer[writeIndex] = number[currentDigitIndex];
writeIndex ++;
DoPrint(wordBuffer, number, currentDigitIndex + 1, writeIndex);
return;
}
foreach (var c in keyPad[number[currentDigitIndex]])
{
wordBuffer[currentDigitIndex] = c;
writeIndex ++;
DoPrint(wordBuffer, number, currentDigitIndex + 1, writeIndex);
writeIndex --;
}
}