-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphonenum-letter-comb.py
47 lines (46 loc) · 1.41 KB
/
phonenum-letter-comb.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
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
numflag = (digits != "") #Checks if digits not empty
inp = [digits]
output = []
endindex = 0
tempdigit = "2"
tempstr = ""
tempindex = 0
while numflag:
numflag = False
inp = inp[endindex:]
print(inp)
endindex = len(inp)
for s in inp:
if s[-1].isdigit():
for i in range(len(s)):
if s[i].isdigit():
numflag = True
tempdigit = s[i]
tempindex = i
break
tempstr = self.chToStr(tempdigit)
for tempch in tempstr:
inp.append(s[:tempindex] + tempch + s[tempindex+1:])
else:
if s not in output:
output.append(s)
return output
def chToStr(self, ch):
if ch == "2":
return "abc"
elif ch == "3":
return "def"
elif ch == "4":
return "ghi"
elif ch == "5":
return "jkl"
elif ch == "6":
return "mno"
elif ch == "7":
return "pqrs"
elif ch == "8":
return "tuv"
elif ch == "9":
return "wxyz"