题目描述
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则按字典序打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
class Solution {
public:
vector<string> Permutation(string str) {
if (str.empty())
return ans;
chang(str,0,str.size());
sort(ans.begin(),ans.end());
auto it=unique(ans.begin(),ans.end());
ans.erase(it,ans.end());
return ans;
}
void chang(string &str,int start ,int len){
if (start ==len)
ans.push_back(str);
for (int i=start;i<len;i++){
swap(str[start],str[i]);
chang(str,start+1,len);
swap(str[start],str[i]);
}
}
vector< string> ans;
};