-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path38.cpp
More file actions
43 lines (42 loc) · 977 Bytes
/
38.cpp
File metadata and controls
43 lines (42 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution
{
private:
set<string> ans;
bool get(int flag, int idx)
{
return (flag >> idx) & 1;
};
void dfs(const string str, string cur, int flag)
{
if (str.size() == cur.size())
{
ans.insert(cur);
return;
};
for (int i = 0; i < str.size(); i++)
{
if (!get(flag, i))
dfs(str, cur + str[i], flag | (1 << i));
};
return;
}
public:
vector<string> permutation(string s)
{
dfs(s, "", 0);
// sort(ans.begin(), ans.end());
// auto iter = ans.begin(); ++iter;
// while (iter != ans.end()) {
// if ((*iter) == *(iter - 1)) ans.erase(iter);
// else iter++;
// };
vector<string> res;
auto iter = ans.begin();
while (iter != ans.end())
{
res.push_back(*iter);
iter++;
};
return res;
};
};