-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreatingstrings.cpp
More file actions
45 lines (35 loc) · 855 Bytes
/
Copy pathcreatingstrings.cpp
File metadata and controls
45 lines (35 loc) · 855 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
44
45
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<string> results;
string S;
vector<bool> used;
void backtrack(string& path) {
if (path.size() == S.size()) {
results.push_back(path);
return;
}
for (int i = 0; i < S.size(); ++i) {
if (used[i]) continue;
// Skip duplicates
if (i > 0 && S[i] == S[i-1] && !used[i-1]) continue;
used[i] = true;
path.push_back(S[i]);
backtrack(path);
path.pop_back();
used[i] = false;
}
}
int main() {
cin >> S;
sort(S.begin(), S.end()); // Required to skip duplicates correctly
used.resize(S.size(), false);
string path;
backtrack(path);
cout << results.size() << '\n';
for (string& perm : results) {
cout << perm << '\n';
}
return 0;
}