Letter Combinations of a Phone Number
LeetCode 17 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
One recursion level per digit.
நான் 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
Empty input → [].
- 2
dfs(index, comb): full length → join and record.
- 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 resLetter 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 [''].