使用getAll()函数即可得到字符串的全部排列,process即为具体的递归过程
class Solution {
public:
vector<string> getAll(string str) {
if (str.empty())return vector<string>();
vector<string>ans;
string t = str;
int index = 0;
process(ans, t, index);
return ans;
}
void process(vector<string>&ans, string t,int index) {
if (index == t.size() - 1) {
ans.push_back(t);
return;
}
else {
for (int i = index; i < t.size(); i++) {
string tmp = t;
swap(tmp[index], tmp[i]);
process(ans, tmp, index + 1);
}
}
}
};