Skip to main content
2 of 2
added 108 characters in body
flornquake
  • 1.6k
  • 11
  • 26

Python, 97 94

from itertools import*
s,n=input()
L=t=[]
exec"t=t+[s];L+=map(''.join,product(*t));"*n
print L

t=t+[s] can't be shortened to t+=[s] because L and t would be pointing to the same list.

Input: 'ab', 3

Output:

['a', 'b', 'aa', 'ab', 'ba', 'bb', 'aaa', 'aab', 'aba', 'abb', 'baa', 'bab', 'bb
a', 'bbb']
flornquake
  • 1.6k
  • 11
  • 26