Letter Combinations of a Phone Number Solutions:
Table of Contents
ToggleDifficulty: Medium
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 digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
![]()
Example 1:
- Input: digits = “23”
- Output: [“ad”,”ae”,”af”,”bd”,”be”,”bf”,”cd”,”ce”,”cf”]
Example 2:
- Input: digits = “”
- Output: []
Example 3:
- Input: digits = “2”
- Output: [“a”,”b”,”c”]
Constraints:
0 <= digits.length <= 4digits[i]is a digit in the range['2', '9'].
Solution:
To solve this problem, we can follow these steps:
- Mapping Creation:
Create an associative array to map each digit to its corresponding letters. - Recursive Function:
Use a recursive function to build combinations. This function will take the current combination being built and the remaining digits to process. - Base Case:
When no more digits are left, add the current combination to the result list. - Recursive Case:
For each letter mapped from the current digit, append it to the current combination and recurse with the remaining digits.
Let’s implement this solution in PHP: 17. Letter Combinations of a Phone Number
<?php
// Test the function
print_r(letterCombinations("23")); // Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
print_r(letterCombinations("")); // Output: []
print_r(letterCombinations("2")); // Output: ["a","b","c"]
?>
Explanation:
- Mapping Creation: An associative array
$phoneMapmaps digits to their corresponding letters. - Recursive Function: The
backtrackfunction builds combinations by appending letters and processing the remaining digits. - Base Case: When no digits are left (
strlen($nextDigits) === 0), the current combination is added to the result. - Recursive Case: For each letter corresponding to the current digit, the function recursively builds the combination.
This approach is efficient given the constraints and handles edge cases like empty input properly. If you have any more questions or need further modifications, let me know!
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Read More

