Letter Combinations of a Phone Number - Leetcode Solution


💡 Step-by-Step Thought Process

  1. Understand the problem: Given a string of digits, return all possible letter combinations based on a phone keypad mapping.
  2. If the input digits string is empty, return an empty list.
  3. Initialize an empty list ans to store the final combinations and a list sol to build the current combination.
  4. Define a letter_map dictionary mapping each digit (2-9) to its corresponding letters (e.g., "2": "abc").
  5. Get the length n of the digits string.
  6. Define a backtrack function that takes an index i, starting from 0.
  7. If i equals n, append the joined sol list to ans as a complete combination.
  8. For each letter in letter_map[digits[i]], append the letter to sol, recursively call backtrack with i + 1, and pop the letter to backtrack.
  9. Call backtrack with index 0 and return ans.

Code Solution


                

                

                

                

Detailed Explanation

Understanding the Problem

The "Letter Combinations of a Phone Number" problem involves generating all possible strings that can be formed by mapping a string of digits (from 2 to 9) to their corresponding letters on a traditional telephone keypad. For example, the digit 2 maps to the letters "abc", 3 maps to "def", and so on.

Given an input string of digits, the goal is to return all valid letter combinations that can be formed by choosing one letter for each digit in the same order. This is a classic problem that emphasizes the importance of backtracking and recursion in generating combinations.

Approach: Recursive Backtracking

Backtracking is the ideal technique for this type of combinatorial problem. At each digit, we have a set of possible letters. Our job is to build every possible string by selecting one letter from each digit’s corresponding letters and combining them in order.

We begin with an empty string and append letters one by one, corresponding to each digit, until we reach the end of the digit string. When we have a complete combination, we add it to the result list. After exploring one possibility, we "backtrack" by removing the last letter and trying the next one.

Step-by-Step Execution

Let's break down the recursive process:

First, we define a mapping from digits to characters using a dictionary: {"2": "abc", "3": "def", ..., "9": "wxyz"}.

We initialize an empty list ans to store all the final combinations and a temporary list sol to build each string during the recursive process.

The backtracking function accepts an index i that tracks which digit we are currently processing. If i reaches the length of the digit string, we know we’ve formed a complete combination and can add it to ans.

Otherwise, we look up the letters for digits[i], append each one to the current solution, recurse to the next digit, and then backtrack by removing the letter we just added.

Time and Space Complexity

The time complexity is O(3n * 4m) where n is the number of digits mapping to 3 letters and m is the number of digits mapping to 4 letters (like '7' or '9'). This represents the total number of combinations we may generate.

The space complexity is also exponential, since we are storing all valid combinations in memory and maintaining a recursive call stack proportional to the number of digits.

Conclusion

This problem is a textbook example of recursive backtracking and is especially valuable for developing an intuition around traversing trees and generating permutations. The solution is clean, elegant, and showcases the power of depth-first recursion when generating complex outputs from simple rules.

Get Personalized Support at AlgoMap Bootcamp 💡