File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution
2
+ {
3
+ public:
4
+ void permutation (string& str, string& temp,vector<string>& strs)
5
+ {
6
+ auto index = temp.size ();
7
+ if (index == str.size ())
8
+ {
9
+ strs.push_back (temp);
10
+ return ;
11
+ }
12
+ std::set<int > cache;
13
+ for (int i = index;i <str.size (); i++)
14
+ {
15
+ auto ch = str[i];
16
+ if (cache.find (ch) == cache.end ())
17
+ {
18
+ cache.insert (ch);
19
+ temp.push_back (ch);
20
+ str[i] = str[index];
21
+ str[index] = ch;
22
+ permutation (str, temp, strs);
23
+ str[index] = str[i];
24
+ str[i] = ch;
25
+ temp.pop_back ();
26
+ }
27
+ }
28
+ }
29
+ vector<string> permutation (string s)
30
+ {
31
+ vector<string> rst;
32
+ string temp;
33
+ temp.resize (s.size ());
34
+ temp.clear ();
35
+ permutation (s, temp, rst);
36
+ return rst;
37
+ }
38
+ };
You can’t perform that action at this time.
0 commit comments