Your helper recursive function should not be called when you are putting the letters in the main list. Move it out of for
loop.
def phoneNumberMnemonics(phoneNumber):
returnList = []
helper(phoneNumber, 0, [], returnList)
return returnList
def helper(phoneNumber, idx, currentList, returnList):
if idx == len(phoneNumber):
returnList.append(currentList)
return
else:
digit = phoneNumber[idx]
letters = hashMap[digit]
for i in letters:
currentList.append(i)
# outside for loop
helper(phoneNumber, idx + 1, currentList, returnList)
hashMap = {'0': ['0'], '1': ['1'], '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']}
print(phoneNumberMnemonics("9056661234"))
OUTPUT
[['w', 'x', 'y', 'z', '0', 'j', 'k', 'l', 'm', 'n', 'o', 'm', 'n', 'o', 'm', 'n', 'o', '1', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']]