Created
August 25, 2020 07:10
-
-
Save arafatkamaal/46a36b70b087538fae1d85c4262f5822 to your computer and use it in GitHub Desktop.
Leetcode, Letter Combinations of a Phone Number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import List | |
class Solution: | |
def __init__(self): | |
self.letter_dict = {"2": "abc", "3": "def", "4": "ghi", "5": "jkl", "6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz"} | |
def letterCombinations(self, digits: str) -> List[str]: | |
if digits == "" or len(digits) == 0: | |
return digits | |
if len(digits) == 1: | |
return list(self.letter_dict[digits[0]]) | |
if len(digits) == 2: | |
result = [] | |
for top in self.letter_dict[digits[0]]: | |
for bottom in self.letter_dict[digits[1]]: | |
result.append(top + bottom) | |
return result | |
result = [] | |
for top in self.letter_dict[digits[0]]: | |
for bottom in self.letterCombinations(digits[1:]): | |
result.append(top + bottom) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment