↓Skip to main content
  1. LeetCode/

LeetCode 17: Letter Combinations of a Phone Number

·2 mins· ·
LeetCode Medium Hash-Table String Backtracking
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-10-20

  • Submissions: Runtime Error ×8 → Accepted → Runtime Error → Accepted ×2

Approach
#

Level i handles digits[i], appending each of its letters to the current string, and records once every digit is handled (Backtracking template). An empty digits returns [], not [""].

My Attempts
#

This took 12 submissions, nearly all Python scoping:

  • The recursion lived in another method that used digits and dic directly, but they were locals of letterCombinations, invisible to the other method (NameError), with a few syntax errors in between.
  • The 2022-11 rewrite stored the mapping as self.d2l and passed digits down as a parameter, and passed.

Solution
#

class Solution:
    def letterCombinations(self, digits: str) -> List[str]:
        if not digits:
            return []
        letters = {"2": "abc", "3": "def", "4": "ghi", "5": "jkl",
                   "6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz"}
        res = []
        def dfs(i, path):
            if i == len(digits):
                res.append(path)
                return
            for c in letters[digits[i]]:
                dfs(i + 1, path + c)
        dfs(0, "")
        return res

Complexity
#

Time O(4^n · n), space O(n).

Mistakes to Avoid
#

  • Another method cannot see this method’s local variables (digits, dic); pass them or use a nested function.
  • An empty string returns [].

Interview Explanation
#

I backtrack one digit per level, appending each of its letters and recording once all digits are processed; an empty input returns an empty list. Time O(4^n · n).

Related

LeetCode 5: Longest Palindromic Substring
·2 mins
LeetCode Medium Two-Pointers String Dynamic-Programming Manacher
LeetCode 5 note: expand around centers, using each position as an odd and an even center; with interval DP, iterate i from large to small
LeetCode 1189: Maximum Number of Balloons
·2 mins
LeetCode Easy Hash-Table String Counting
LeetCode 1189 note: each letter supports count // needed balloons, with l and o needed twice; take the minimum
LeetCode 1002: Find Common Characters
·1 min
LeetCode Easy Array Hash-Table String
LeetCode 1002 note: count each word and intersect all Counters (minimum count per character), then expand
LeetCode 929: Unique Email Addresses
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 929 note: split at @, normalize only the local part (drop everything after +, remove dots), rejoin with the domain and collect in a set
LeetCode 953: Verifying an Alien Dictionary
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 953 note: map letters to their alien index and compare each adjacent pair at the first differing character; with no difference, the shorter must come first
LeetCode 884: Uncommon Words from Two Sentences
·1 min
LeetCode Easy Hash-Table String Counting
LeetCode 884 note: count words across both sentences together; those appearing exactly once are the answer