logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramtwitterfacebookyoutubelinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
Table Of Contents
2. Add Two Numbers3. Longest Substring Without Repeating Characters5. Longest Palindromic Substring8. String to Integer (atoi)11. Container With Most Water15. 3Sum17. Letter Combinations of a Phone Number19. Remove Nth Node From End of List22. Generate Parentheses33. Search in Rotated Sorted Array34. Find First and Last Position of Element in Sorted Array48. Rotate Image49. Group Anagrams50. Pow(x, n)53. Maximum Subarray54. Spiral Matrix56. Merge Intervals61. Rotate List73. Set Matrix Zeroes74. Search a 2D Matrix75. Sort Colors78. Subsets137. Single Number II138. Copy List with Random Pointer150. Evaluate Reverse Polish Notation151. Reverse Words in a String153. Find Minimum in Rotated Sorted Array155. Min Stack162. Find Peak Element169. Majority Element198. House Robber207. Course Schedule215. Kth Largest Element in an Array227. Basic Calculator II236. Lowest Common Ancestor of a Binary Tree237. Delete Node in a Linked List238. Product of Array Except Self451. Sort Characters By Frequency540. Single Element in a Sorted Array560. Subarray Sum Equals K636. Exclusive Time of Functions875. Koko Eating Bananas1011. Capacity To Ship Packages Within D Days1101. The Earliest Moment When Everyone Become Friends1283. Find the Smallest Divisor Given a Threshold2140. Solving Questions With Brainpower3169. Count Days Without Meetings3481. Apply Substitutions
Code Scratch/leetcode/medium

17. Letter Combinations of a Phone Number

2 min read
On this pageIntuition
IntuitionComplexityCode

# Intuition

Each digit from 2 to 9 maps to a set of letters on a traditional phone keypad. To find all combinations of letters the input digits could represent, we treat this like a combinatorial traversal problem — specifically, a depth-first search (DFS) where we build each possible combination character-by-character.

# Complexity

Space ComplexityTime Complexity
O(1)\text{O}(1)O(1)O(4N)\text{O}(4^N)O(4N)

# Code

// Mapping of digits to corresponding letters (like a phone keypad)
Map&lt;Character, char[]&gt; digitToLettersMap = new HashMap<>();
 
// List to store all possible combinations
List&lt;String&gt; allCombinations = new LinkedList<>();
 
// Recursive backtracking helper function
void generateCombinations(String inputDigits, int currentIndex, StringBuilder currentCombination) {
    // Base case: if we've built a full combination, add it to the result list
    if (currentIndex == inputDigits.length()) {
        allCombinations.add(currentCombination.toString());
        return;
    }
 
    // Get the letters corresponding to the current digit
    char currentDigit = inputDigits.charAt(currentIndex);
    for (char letter : digitToLettersMap.get(currentDigit)) {
        // Choose
        currentCombination.append(letter);
 
        // Explore
        generateCombinations(inputDigits, currentIndex + 1, currentCombination);
 
        // Un-choose (backtrack)
        currentCombination.deleteCharAt(currentCombination.length() - 1);
    }
}
 
public List&lt;String&gt; letterCombinations(String digits) {
    // Edge case: no input
    if (digits.isEmpty()) return Collections.emptyList();
 
    // Initialize the digit-to-letter mapping
    digitToLettersMap.put('2', new char[]{'a', 'b', 'c'});
    digitToLettersMap.put('3', new char[]{'d', 'e', 'f'});
    digitToLettersMap.put('4', new char[]{'g', 'h', 'i'});
    digitToLettersMap.put('5', new char[]{'j', 'k', 'l'});
    digitToLettersMap.put('6', new char[]{'m', 'n', 'o'});
    digitToLettersMap.put('7', new char[]{'p', 'q', 'r', 's'});
    digitToLettersMap.put('8', new char[]{'t', 'u', 'v'});
    digitToLettersMap.put('9', new char[]{'w', 'x', 'y', 'z'});
 
    // Begin backtracking from index 0 with an empty combination
    generateCombinations(digits, 0, new StringBuilder());
 
    return allCombinations;
}
 
←Previous15. 3SumNext→19. Remove Nth Node From End of List

On this page

IntuitionComplexityCode