Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 25ae478

Browse files
Letter Combinations of a Phone Number
1 parent 1740be9 commit 25ae478

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''Leetcode- https://leetcode.com/problems/letter-combinations-of-a-phone-number/ '''
2+
'''
3+
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
4+
5+
A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
6+
7+
Example 1:
8+
9+
Input: digits = "23"
10+
Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
11+
12+
'''
13+
14+
def letterCombinations(digits):
15+
res = []
16+
digitToChar = { "2": "abc",
17+
"3": "def",
18+
"4": "ghi",
19+
"5": "jkl",
20+
"6": "mno",
21+
"7": "qprs",
22+
"8": "tuv",
23+
"9": "wxyz"}
24+
25+
def backtrack(i,cur):
26+
if len(cur) == len(digits):
27+
res.append(cur)
28+
return
29+
30+
for j in digitToChar[digits[i]]:
31+
backtrack(i+1,cur+j)
32+
33+
if digits:
34+
backtrack(0,"")
35+
36+
return res
37+
38+
#T:O(n.4^n)
39+
#S:O(n)
40+
41+

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ Check the notes for the explaination - [Notes](https://stingy-shallot-4ea.notion
1818
- [x] [Combination Sum III](Backtracking/216-Combination-Sum-III.py)
1919
- [x] [Generate Parentheses](Backtracking/22-Generate-Parentheses.py)
2020
- [x] [Palindrome Partitioning](Backtracking/131-Palindrome-Partitioning.py)
21+
- [x] [Letter Combinations of a Phone Number](Backtracking/17-Letter-Combinations-of-a-Phone-Number.py)
2122

2223

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /