-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
38 lines (31 loc) · 914 Bytes
/
main.cpp
File metadata and controls
38 lines (31 loc) · 914 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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> permute(string str);
void permuteHelper(int n, vector<char>& buffer, vector<string>& result);
vector<string> permute(string str) {
vector<string> result;
vector<char> buffer {str.begin(), str.end()};
permuteHelper(str.length(), buffer, result);
return result;
}
void permuteHelper(int n, vector<char>& buffer, vector<string>& result) {
if (n == 1) {
result.push_back(string(buffer.begin(), buffer.end()));
return;
}
for (int i = 0; i < n - 1; i++) {
permuteHelper(n - 1, buffer, result);
std::swap(buffer[n - 1], (n % 2) == 1 ? buffer[0] : buffer[i]);
}
permuteHelper(n - 1, buffer, result);
}
int main() {
auto vec = permute("aba");
for (auto i: vec) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}