I am trying to print all possible permutations of a string of different lengths
I am doing
def toString(List):
return ''.join(List)
def permute(string1, l, r):
if l == r:
print(toString(string1))
else:
for i in range(l, r + 1):
string1[l], string1[i] = string1[i], string1[l]
permute(string1, l + 1, r)
string1[l], string1[i] = string1[i], string1[l]
string = "ABC"
n = len(string)
a = list(string)
permute(a, 0, n-1)
but it returns ABC ACB BAC BCA CBA CAB
I want it to return A, B, C, AB, BC, AC, ABC, ACB, BAC etc.
I am unable to achieve that