-
Notifications
You must be signed in to change notification settings - Fork 0
/
letter_combinations_of_a_phone_number.py
47 lines (39 loc) · 1.36 KB
/
letter_combinations_of_a_phone_number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from typing import List
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
if not digits:
return []
phone = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz"
}
def backtrack(index, path):
if index == len(digits):
combinations.append("".join(path))
return
for letter in phone[digits[index]]:
path.append(letter)
backtrack(index + 1, path)
path.pop()
combinations = []
backtrack(0, [])
return combinations
# Time complexity: O(3^N * 4^M) where N is the number of digits in the input that maps to 3 letters (e.g. 2, 3, 4, 5, 6, 8) and M is the number of digits in the input that maps to 4 letters (e.g. 7, 9)
# Space complexity: O(3^N * 4^M) since one has to keep 3^N * 4^M solutions
import unittest
class TestSolution(unittest.TestCase):
def setUp(self) -> None:
self.solution = Solution()
def test_example1(self) -> None:
self.assertEqual(
self.solution.letterCombinations("23"),
["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
)
if __name__ == '__main__':
unittest.main()