Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 17 · Combinations

Letter Combinations of a Phone Number

LeetCode 17 · మధ్యస్థం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

One recursion level per digit.

మధ్యస్థంHash TableStringBacktracking
LeetCode లో తెరవండి ↗

నేను Letter Combinations of a Phone Number (LeetCode 17) ను ఇలా పరిష్కరించాను — Combinations ప్యాటర్న్‌లోని ఒక మధ్యస్థం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.

కింది వివరణ ఇంగ్లీష్‌లో ఉంది.

Intuition

Each digit offers 3–4 letters. Walk the digits left to right, trying each letter for the current digit and recursing on the next; a full-length combination is one answer.

Approach

  1. 1

    Empty input → [].

  2. 2

    dfs(index, comb): full length → join and record.

  3. 3

    For each letter of digits[index], append, recurse, pop.

నేను ఎలా పరిష్కరించాను

BacktrackingPythonO(4ⁿ·n) time
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", #4
            "8": "tuv",
            "9": "wxyz", #4
        }
        res = []
        def dfs(index,comb):
            if len(comb)==len(digits):
                res.append("".join(comb))
                return
            cur_digit = digits[index]
            cur_letters = letters[cur_digit]
            for letter in cur_letters:
                comb.append(letter)
                dfs(index+1,comb)
                comb.pop()
        dfs(0,[])
        return res
స్టెప్-త్రూ లోడ్ అవుతోంది…

Letter Combinations of a Phone Number — తరచూ అడిగే ప్రశ్నలు

Letter Combinations of a Phone Number వెనుక ఉన్న intuition ఏమిటి?

Each digit offers 3–4 letters. Walk the digits left to right, trying each letter for the current digit and recursing on the next; a full-length combination is one answer.

Letter Combinations of a Phone Number (LeetCode 17) ను ఎలా పరిష్కరించాలి?

1. Empty input → []. 2. dfs(index, comb): full length → join and record. 3. For each letter of digits[index], append, recurse, pop.

Letter Combinations of a Phone Number యొక్క time, space complexity ఎంత?

Time: O(4ⁿ · n). Space: O(n). (Accepted approach: Backtracking.)

Letter Combinations of a Phone Number లో సాధారణ పొరపాట్లు ఏమిటి?

Empty input must return [] rather than [''].