Tutorialspoint
Problem
Solution
Submissions

Letter Combinations of a Phone Number

Certification: Intermediate Level Accuracy: 0% Submissions: 0 Points: 10

Write a C program to find all possible letter combinations that the numbers of a given string could represent on a telephone keypad. Given a string containing digits from 2-9, return all possible letter combinations that the number could represent. The mapping of digits to letters is the same as on telephone buttons (as shown below): - 2: abc - 3: def - 4: ghi - 5: jkl - 6: mno - 7: pqrs - 8: tuv - 9: wxyz

Example 1
  • Input: digits = "23"
  • Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
  • Explanation:
    The digit '2' corresponds to letters 'a', 'b', 'c'. The digit '3' corresponds to letters 'd', 'e', 'f'.
    All possible combinations are: "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf".
Example 2
  • Input: digits = "5"
  • Output: ["j", "k", "l"]
  • Explanation:
    The digit '5' corresponds to letters 'j', 'k', 'l'.
    Since there is only one digit, the combinations are simply the letters themselves.
Constraints
  • 0 <= digits.length <= 4
  • digits[i] is a digit in the range ['2', '9']
  • Time Complexity: O(4^n) where n is the length of digits
  • Space Complexity: O(n) for recursion stack
NumberControl StructuresMicrosoftTutorialspoint
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use a mapping of digits to their corresponding letters
  • Use backtracking or recursion to generate all possible combinations
  • For each digit, try all its corresponding letters and proceed to the next digit
  • Use a temporary string to build each combination
  • When the combination's length equals the input's length, add it to the result

Steps to solve by this approach:

 Step 1: Create a mapping from digits to their corresponding letters on a phone keypad.
 Step 2: Use a recursive backtracking approach to generate all possible combinations.
 Step 3: Calculate the maximum possible number of combinations to allocate memory for the result.
 Step 4: For each digit, try each of its corresponding letters.
 Step 5: Build the combination string character by character during recursion.
 Step 6: When we reach the end of the digits string, add the current combination to our result.
 Step 7: Handle edge cases like empty input strings, and properly manage memory allocation and deallocation.

Submitted Code :