Skip to content

Instantly share code, notes, and snippets.

@arafatkamaal
Created August 25, 2020 07:10
Show Gist options
  • Save arafatkamaal/46a36b70b087538fae1d85c4262f5822 to your computer and use it in GitHub Desktop.
Save arafatkamaal/46a36b70b087538fae1d85c4262f5822 to your computer and use it in GitHub Desktop.
Leetcode, Letter Combinations of a Phone Number
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