programming-examples/python/Math/Print all permutations of a given string.py

13 lines
454 B
Python
Raw Normal View History

2019-11-15 12:59:38 +01:00
def permute_string(str):
if len(str) == 0:
return ['']
prev_list = permute_string(str[1:len(str)])
next_list = []
for i in range(0,len(prev_list)):
for j in range(0,len(str)):
new_str = prev_list[i][0:j]+str[0]+prev_list[i][j:len(str)-1]
if new_str not in next_list:
next_list.append(new_str)
return next_list
print(permute_string('ABCD'));