Skip to content

Commit 9a3312e

Browse files
april 8
1 parent ed7a971 commit 9a3312e

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

Challenges/2021/April-LeetCoding-Challenge.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,10 @@ None
2121
|1551.|[Minimum Operations to Make Array Equal](https://leetcode.com/problems/minimum-operations-to-make-array-equal/)|[Python](/Medium/1551.MinimumOperationstoMakeArrayEqual.py)|Medium|
2222
|1704.|[Determine if String Halves Are Alike](https://leetcode.com/problems/determine-if-string-halves-are-alike/)|[Python](/Easy/1704.DetermineifStringHalvesAreAlike.py)|Easy|
2323

24+
## Week 2
25+
||Title|Solution|Difficulty|
26+
| ----: | --- | --- | --- |
27+
|17.|[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)|[Python](/Medium/17.LetterCombinationsofaPhoneNumber.py)|Medium|
28+
2429
## License
2530
The code is open-source and licensed under the [MIT License](/LICENSE).
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'''
2+
Given a string containing digits from 2-9 inclusive,
3+
return all possible letter combinations that the number
4+
could represent. Return the answer in any order.
5+
6+
A mapping of digit to letters (just like on the
7+
telephone buttons) is given below. Note that 1 does not
8+
map to any letters.
9+
10+
Example:
11+
Input: digits = "23"
12+
Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
13+
14+
Example:
15+
Input: digits = ""
16+
Output: []
17+
18+
Example:
19+
Input: digits = "2"
20+
Output: ["a","b","c"]
21+
22+
Constraints:
23+
- 0 <= digits.length <= 4
24+
- digits[i] is a digit in the range ['2', '9'].
25+
'''
26+
#Difficulty: Medium
27+
#25 / 25 test cases passed.
28+
#Runtime: 28 ms
29+
#Memory Usage: 14.2 MB
30+
31+
#Runtime: 28 ms, faster than 81.79% of Python3 online submissions for Letter Combinations of a Phone Number.
32+
#Memory Usage: 14.2 MB, less than 86.13% of Python3 online submissions for Letter Combinations of a Phone Number.
33+
34+
class Solution:
35+
def letterCombinations(self, digits: str) -> List[str]:
36+
length = len(digits)
37+
letters = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}
38+
return self.dfs(digits, letters, length, [], '', 0)
39+
40+
def dfs(self, digits, letters, length, result, combination, current):
41+
if current == length:
42+
result.append(combination)
43+
return
44+
for char in letters[digits[current]]:
45+
self.dfs(digits, letters, length, result, combination+char, current+1)
46+
return result

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Python solutions of LeetCode problems.
22
![Language](https://img.shields.io/badge/language-Python-blue.svg)&nbsp;
3-
![Problems Solved](https://img.shields.io/badge/problems%20solved-531%2F1637-orange)&nbsp;
3+
![Problems Solved](https://img.shields.io/badge/problems%20solved-532%2F1637-orange)&nbsp;
44
[![License](https://img.shields.io/badge/license-MIT-green.svg)](./LICENSE)&nbsp;
55
![Update](https://img.shields.io/badge/update-Daily-brightgreen.svg)&nbsp;
66
<br><br>
@@ -22,7 +22,7 @@ In this repository provided my Python solutions of LeetCode problems.
2222
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 27/31
2323
- [February LeetCoding Challenge](/Challenges/2021/February-LeetCoding-Challenge.md) - 23/28
2424
- [March LeetCoding Challenge](/Challenges/2021/March-LeetCoding-Challenge.md) - 23/31
25-
- [April LeetCoding Challenge](/Challenges/2021/April-LeetCoding-Challenge.md) - 5/30
25+
- [April LeetCoding Challenge](/Challenges/2021/April-LeetCoding-Challenge.md) - 6/30
2626
<br><br>
2727
## Solutions
2828
*P.S. If you like this, please leave me a star.*
@@ -42,6 +42,7 @@ In this repository provided my Python solutions of LeetCode problems.
4242
|14.|[Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)|[Python](https://github.com/YuriSpiridonov/LeetCode/blob/master/Easy/14.LongestCommonPrefix.py)|Easy||
4343
|15.|[3Sum](https://leetcode.com/problems/3sum/)|[Python](/Medium/15.3Sum(bruteforce).py)|Medium|Brute Force|
4444
|16.|[3Sum Closest](https://leetcode.com/problems/3sum-closest/)|[Python](/Medium/16.3SumClosest(bruteforce).py)|Medium|Brute Force|
45+
|17.|[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)|[Python](/Medium/17.LetterCombinationsofaPhoneNumber.py)|Medium|`DFS`, `Recursion`|
4546
|19.|[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)|[Python](/Medium/19.RemoveNthNodeFromEndofList.py)|Medium|`Stack`|
4647
|20.|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)|[Python](/Easy/20.ValidParentheses(BruteForce).py)|Easy|Brute Force|
4748
|20.|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)|[Python](/Easy/20.ValidParentheses(Stack).py)|Easy|`Stack`|

0 commit comments

Comments
 (0)