I am trying to solve this: https://leetcode.com/problems/letter-combinations-of-a-phone-number/ but when appending the value to the first list inside the list it append it also to the 4th and the 7th as they are still counted with the first list. Is it due to the fact that I use c *= len(s[i])? How can I solve the problem?
def g(s):
s = [int(i) for i in s]
print(s)
m = len(s)
num = 0
while m>0:
if s[num] == 2:
s[num] = ["a","b","c"]
elif s[num] == 3:
s[num] = ["d","e","f"]
elif s[num] == 4:
s[num] = ["g","h","i"]
elif s[num] == 5:
s[num] = ["j","k","l"]
elif s[num] == 6:
s[num] = ["m","n","o"]
elif s[num] == 7:
s[num] = ["p","q","r", "s"]
elif s[num] == 8:
s[num] = ["t","u","v"]
elif s[num] == 9:
s[num] = ["w","x","y", "z"]
else:
s[num] = [" "]
m -=1
num +=1
c = [[i] for i in s[0]]
for i in range(1,len(s)):
print("i",i)
c *= len(s[i])
print(c[0])
for j in range(len(s[i])):
print("j",j)
if len(s[i])==3:
for k in range(j*3, (j*3)+len(s[i])):
print("k",k)
c[k].append(s[i][j])
print(c)
else:
for k in range(j*4, (j*4)+len(s[i])):
print("k",k)
c[k].append(s[i][j])
print(c)
return c
You'll find itertools useful for this.
Start with a constant dictionary. No need to convert the digits to integers.
This should work...