How to get all letter combinations of a phone number given a string of digits from 2-9 in C#

2 Answers

0 votes
using System;
using System.Collections.Generic;
 
// Letter Combinations of a Phone Number
// Given a string containing digits from 2-9, 
// get all possible letter combinations that a phone number represents
 
public class Program
{
    public static List<string> LetterCombinationsOfPhoneNumber(string digits) {
        int total_digits = digits.Length;
         
        string[] arr = new string[10]; // 0 - 9
        arr[2] = "abc";
        arr[3] = "def";
        arr[4] = "ghi";
        arr[5] = "jkl";
        arr[6] = "mno";
        arr[7] = "pqrs";
        arr[8] = "tuv";
        arr[9] = "wxyz";
         
        List<string> lst = new();
         
        if (total_digits == 0)
            return lst;
         
        int l1 = digits[0] - '0';
        foreach(var ch1 in arr[l1]) { // 1
            if (total_digits > 1) {
                int l2 = digits[1] - '0';
                foreach (var ch2 in arr[l2]) { // 2
                    if (total_digits > 2) {
                        int l3 = digits[2] - '0';
                        foreach (var ch3 in arr[l3]) { // 3
                            if (total_digits > 3) {
                                int l4 = digits[3] - '0';
                                foreach (var ch4 in arr[l4]) { // 4
                                    lst.Add(ch1.ToString() + ch2.ToString() + ch3.ToString() + ch4.ToString());
                                }
                            }
                            else {
                                lst.Add(ch1.ToString() + ch2.ToString() + ch3.ToString());
                            }
                        }
                    }
                    else {
                        lst.Add(ch1.ToString() + ch2.ToString());
                    }
                }
            }
            else {
                lst.Add(ch1.ToString());
            }
        }
        return lst;
    }
     
    public static void Main(string[] args)
    {
        List<String> lst = LetterCombinationsOfPhoneNumber("23");
         
        Console.WriteLine(string.Join(", ", lst));
    }
}
 
 
 
/*
run:
 
ad, ae, af, bd, be, bf, cd, ce, cf
 
*/

 



answered Mar 3, 2024 by avibootz
edited Apr 25, 2025 by avibootz
0 votes
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 

*/

 



answered Apr 25, 2025 by avibootz
...