-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm0017.py
52 lines (39 loc) · 1.37 KB
/
m0017.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
48
49
50
51
52
"""Letter Combinations of a Phone Number
TAG: back-tracking
Given a string containing only numbers from 2 to 9, return all the alphabetic
combinations. The mapping from letters to numbers is the same with phone keys.
Examples:
* input:"23"
* output:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
"""
from itertools import product
from typing import Tuple
class PhoneLetters(object):
DC = {
'2': ('a', 'b', 'c'),
'3': ('d', 'e', 'f'),
'4': ('g', 'h', 'i'),
'5': ('j', 'k', 'l'),
'6': ('m', 'n', 'o'),
'7': ('p', 'q', 'r', 's'),
'8': ('t', 'u', 'v'),
'9': ('w', 'x', 'y', 'z')
}
@staticmethod
def _cartesian_product(seq1: Tuple[str, ...],
seq2: Tuple[str, ...]) -> Tuple[str, ...]:
return tuple(i + j for i, j in product(seq1, seq2))
@classmethod
def solution(cls, s: str):
def helper(seq: Tuple[str, ...], acc: Tuple[str, ...]):
if not seq:
return acc
else:
return helper(seq[1:],
cls._cartesian_product(acc, cls.DC[seq[0]]))
return set(helper(tuple(c for c in s), ('', )))
if __name__ == '__main__':
input_1 = '23'
exp_1 = {'ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf'}
pl = PhoneLetters()
assert pl.solution(input_1) == exp_1