using System;
using System.Collections.Generic;
class Program
{
// Mapping digits to letters for the phone number combinations
List<string> letters = new List<string> {
"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"
};
List<string> result = new List<string>();
// Method to print the combinations
void Print(List<string> lst) {
foreach (string n in lst) {
Console.Write(n + " ");
}
Console.WriteLine();
}
// Recursive method to generate combinations
void CreateCombinations(string digits, string output, int di) {
if (digits.Length == di) {
result.Add(output);
return;
}
for (int i = 0; i < letters[digits[di] - '2'].Length; i++) {
// Adding current character to the output
output += letters[digits[di] - '2'][i];
CreateCombinations(digits, output, di + 1);
// Remove last character to backtrack
output = output.Substring(0, output.Length - 1);
}
}
// Method to get all letter combinations of a phone number
List<string> LetterCombinationsOfPhoneNumber(string digits) {
if (digits == "") {
return result;
}
string output = "";
CreateCombinations(digits, output, 0);
return result;
}
static void Main(string[] args)
{
Program p = new Program();
List<string> result = p.LetterCombinationsOfPhoneNumber("23");
p.Print(result);
}
}
/*
run:
ad ae af bd be bf cd ce cf
*/