Skip to content
Hello Python

Checking your account…

Sign in to save your code and progress across devices. The lesson and problem statement remain public.

Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace

Problem

Implement Solution.letterCombinations(digits). Digits are from 2 through 9. Return every possible mapped letter string; return [] for empty input. Output order does not matter.

Starter code

class Solution:
    def letterCombinations(self, digits: str) -> list[str]:
        pass
Test cases

two-digits

{
  "args": [
    "23"
  ]
}

Expected: ["ad","ae","af","bd","be","bf","cd","ce","cf"]

Wizard outline
  1. Step 1: Initialize Solution.letterCombinations

    Replace the empty starter with the first real state owned by Solution.letterCombinations. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.

  2. Step 2: Assemble the primary transition

    Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.

  3. Step 3: Pass the Two Digits case

    Complete the readable core algorithm for one representative Interview case. Model one recursive decision per input digit and emit only complete paths.

  4. Step 4: Harden the Four Letter Key boundary

    Repair the reviewed boundary and pass the complete submission contract. At depth i the path contains exactly one mapped letter for each of the first i digits; branching over every mapping and emitting at full depth produces every valid combination exactly once.

Footguns and prerequisites
  • An empty digit string has no combinations, rather than one empty-string combination.
  • recursion and backtracking
Reviewed references
Practice prerequisites
  • Complete One Backtracking Frame(opens in a new tab)

    Complete One Backtracking Frame isolates before expanding each choice, path equals the original caller-owned prefix; every emitted candidate contains exactly one additional element. That focused state discipline is required when implementing letter combinations phone as a complete Interview Problem.

Recommended approach and implementation

Backtrack by digit index, append each mapped letter, then remove it before trying the next letter.

Why it works: At depth i the path contains exactly one mapped letter for each of the first i digits; branching over every mapping and emitting at full depth produces every valid combination exactly once.

class Solution:
    def letterCombinations(self, digits):
        """
        Checkpoint 1: initialize the state owned by this Interview contract.
        Checkpoint 2: assemble the primary transition without hiding the boundary.
        """
        if not digits:
            return []
        mapping = {
            '2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl',
            '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz',
        }
        result = []
        path = []
        def backtrack(index):
            if index == len(digits):
                result.append(''.join(path))
                return
            for letter in mapping[digits[index]]:
                path.append(letter)
                backtrack(index + 1)
                path.pop()
        backtrack(0)
        return result
Similar exercises