原题目:https://leetcode-cn.com/problems/permutation-ii-lcci/
思路:
使用map记录字符的个数,然后回溯遍历
代码:
class Solution {
vector<string> ans;
unordered_map<char,int> m;
void dfs(int n,string tmp){
if(n==0){
ans.push_back(tmp);return;
}
for(auto &t:m){
if(t.second){
t.second--;
dfs(n-1,tmp+t.first);
t.second++;
}
}
}
public:
vector<string> permutation(string S) {
for (char& i: S) m[i] ++;
int n = S.size();
dfs(n,"");
return ans;
}
};