输入一个字符串,按字典序打印出该字符串中字符的所有排列。
例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
结果请按字母顺序输出。
输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
class Solution:
def Permutation(self, ss):
if not len(ss):
return []
if len(ss) == 1:
return list(ss)
charList = list(ss)
charList.sort()
pStr = []
for i in range(len(charList)):
if i > 0 and charList[i] == charList[i-1]:
continue
temp = self.Permutation(''.join(charList[:i])+''.join(charList[i+1:]))
for j in temp:
pStr.append(charList[i]+j)
return pStr
# 扩展习题, 生成字符的所有组合
# 比如输入abc, 则他们的组合有['a', 'ab', 'abc', 'ac', 'b', 'bc', 'c'], ab和ba属于不同的排列, 但属于同一个组合
def group(self, ss):
if not len(ss):
return []
if len(ss) == 1:
return list(ss)
charList = list(ss)
charList.sort()
pStr = []
for i in range(len(charList)):
pStr.append(charList[i])
if i > 0 and charList[i] == charList[i - 1]:
continue
temp = self.group(''.join(charList[i + 1:]))
for j in temp:
pStr.append(charList[i] + j)
pStr = list(set(pStr))
pStr.sort()
return pStr
package main
import (
"fmt"
)
func stringPermutation(str string) []string {
var res []string
s := []byte(str)
length := len(s)
var dfs func(idx int)
dfs = func(idx int){
if idx == length {
str := string(s)
res = append(res, str)
return
}
m := make(map[byte]bool)
for i:=idx; i<length; i++{
if _,ok := m[s[i]]; ok { continue }
m[s[i]] = true
s[idx], s[i] = s[i], s[idx]
dfs(idx+1)
s[i], s[idx] = s[idx], s[i]
}
}
dfs(0)
return res
}
网友评论