All possible strings of any length that can be formed from a given string
Input:
abc
Output:
a b c abc ab ac bc bac bca
cb ca ba cab cba acb
I have tried using this but it's limited to string abc, I want to generalize it like if input 'abcd' i will provide me output for the same.
def perm_main(elems):
perm=[]
for c in elems:
perm.append(c)
for i in range(len(elems)):
for j in range(len(elems)):
if perm[i]!= elems[j]:
perm.append(perm[i]+elems[j])
level=[elems[0]]
for i in range(1,len(elems)):
nList=[]
for item in level:
#print(item)
nList.append(item+elems[i])
#print(nList)
for j in range(len(item)):
#print(j)
nList.append(item[0:j]+ elems[i] + item[j:])
#print(nList)
level=nList
perm = perm + nList
return perm