伪代码
Python代码
def get_permutations(sequence):
if len(sequence) == 1:
return [sequence]
else:
permutations = []
first_letter = sequence[0]
rest_of_sequence = sequence[1:]
for permutation in get_permutations(rest_of_sequence):
for index in range(len(permutation)+1):
#去除重复的排列
if permutation[:index] + first_letter + permutation[index:] in permutations:
continue
permutations.append(permutation[:index] + first_letter + permutation[index:])
return permutations
example_input = 'test'
print(get_permutations(example_input))
输出结果:
test
etst
estt
tset
stet
sett
tste
stte
tets
etts
ttes
ttse