数据结构(剑指offer)(字符串全排列)(python)

题目描述

输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。

输入描述:

输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
# -*- coding:utf-8 -*-
import itertools
class Solution:
    def Permutation(self, ss):
        # write code here
        if not ss:
            return []
        return sorted(list(set(map(''.join, itertools.permutations(ss)))))
Int[82]: set(map(''.join, itertools.permutations(['a','v','c'],3)))
Out[82]: {'acv', 'avc', 'cav', 'cva', 'vac', 'vca'}

for i in itertools.permutations([1, 2, 3], 3):
    print (i)
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)

猜你喜欢

转载自blog.csdn.net/weixin_41643439/article/details/87212525