I am trying to solve the following leetcode problem: Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
Here is my code in python:
digits = dict()
digits[2] = ['a', 'b', 'c']
digits[3] = ['d', 'e', 'f']
digits[4] = ['g', 'h', 'i']
digits[5] = ['j', 'k', 'l']
digits[6] = ['m', 'n', 'o']
digits[7] = ['p', 'q', 'r', 's']
digits[8] = ['t', 'u', 'v']
digits[8] = ['w', 'x', 'y', 'z']
st = [4, 2, 5]
def recur(st):
if len(st) == 1:
return digits[st[0]]
else:
for i in st:
for j in digits[i]:
x = recur(st[1:])
for z in x:
print(j + z)
recur(st)
It works pretty fine with 2 variables but when I add an extra one, TypeError occurs. Please, give me the tip of what may be wrong.
TypeError Traceback (most recent call last)
<ipython-input-2-f5b34b89f5ce> in <module>
21 print(j + z)
22
---> 23 recur(st)
<ipython-input-2-f5b34b89f5ce> in recur(st)
18 for j in digits[i]:
19 x = recur(st[1:])
---> 20 for z in x:
21 print(j + z)
22
TypeError: 'NoneType' object is not iterable