Letter Combinations of a Phone Number Leetcode Solution

Letter Combinations of a Phone Number Leetcode Problem :

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.

A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

 Letter Combinations of a Phone Number Leetcode Solution :

Constraints :

  • 0 <= digits.length <= 4
  • digits[i] is a digit in the range [‘2’, ‘9’].

Example 1:

Input: digits = “”

Output: []

Example 2:

Input: digits = “2”

Output: [“a”,”b”,”c”]

Idea :

The intuition behind this code is to use a recursive approach to generate all possible combinations. The function solve is the recursive function that does the job.


  1. The function letter Combinations takes a string digits as input, representing a sequence of digits (e.g., “23” or “456”).
  2. It initializes an unordered map ‘mp’, which maps each digit to the corresponding letters on a phone keypad. For example, ‘2’ maps to “abc”, ‘3’ maps to “def”, and so on.
  3. A vector ans is declared to store the final result, i.e., all possible letter combinations.
  4. A string temp is used to store the temporary letter combination being constructed during each recursive call.
  5. The solve function is a recursive backtracking function. It takes an index idx, the ans vector, the digits string, the mp mapping, and the temp string.
  6. The base case of the recursion is when the idx reaches the end of the digits string. At this point, the temp string contains a valid letter combination, so it is added to the ans vector, and the function returns.
  7. If the base case is not met, the function processes the current digit at index idx. It retrieves the corresponding string of letters from the mp mapping and iterates through each letter.
  8. For each letter, it appends it to the temp string, and then makes a recursive call to solve with the next index (idx+1) to process the next digit in the digits string.
  9. After the recursive call returns, the last character is removed from the temp string using temp.pop_back(). This backtracking step is essential to explore all possible combinations correctly.
  10. The function letter Combinations initializes the temp string as an empty string and starts the recursion by calling solve(0, ans, digits, mp, temp).
  11. Finally, it returns the ans vector containing all the possible letter combinations.


Time complexity:

Space complexity:
O(N^2 + 3^N)


Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Code :

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription