-
Notifications
You must be signed in to change notification settings - Fork 15
/
Permutation_of_String.cpp
63 lines (49 loc) · 1.19 KB
/
Permutation_of_String.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <bits/stdc++.h>
using namespace std;
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define time cerr << "Time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" \
<< "\n";
#define F first
#define S second
#define pb push_back
typedef long long int ll;
void permutation(string s, string answer)
{
if (s.length() == 0)
{
cout << answer << "\n";
return;
}
for (int i = 0; i < (int)s.length(); i++)
{
char first = s[i];
string left_String = s.substr(0, i);
string right_String = s.substr(i + 1);
string rest_String = left_String + right_String;
permutation(rest_String, answer + first);
}
}
void solve()
{
string s = "PQRS";
string answer = "";
permutation(s, answer);
/* Time complexity = O(n*n!)
n! for permutations
O(n) time to print each permutation
*/
}
int32_t main()
{
fast;
time;
int t = 1;
//cin >> t;
while (t--)
{
solve();
}
return 0;
}