VISUALGO//PROBLEMS
letter combinations of a phone number · backtrackingmedium
LeetCode #17
digits
2
3
keys
2abc
3def
path
·
result · 0 / 9
ELEMENTS
digits
Python
def letterCombinations(digits: str) -> list[str]:
    if not digits:
        return []

    phone = {
        "2": "abc", "3": "def", "4": "ghi", "5": "jkl",
        "6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz",
    }
    result = []

    def backtrack(i: int, path: list[str]) -> None:
        if i == len(digits):
            result.append("".join(path))
            return

        for letter in phone[digits[i]]:
            path.append(letter)
            backtrack(i + 1, path)
            path.pop()

    backtrack(0, [])
    return result