forked from shreyamalogi/DSA-BOOK
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path057_permutations of a string.cpp
69 lines (57 loc) · 1.07 KB
/
057_permutations of a 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
64
65
66
67
68
69
//author :shreyamalogi
//permutations of a given string
//can be done with stl
//but if that is not allowed then
//#include<bits/stdc++.h>
//
//using namespace std;
//
//int main()
//{
// int t;
// cin>>t;
// while(t--)
// {
// string s;
// cin>>s;
// int n= s.length();
//
// sort(s.begin(),s.end());
// do{
// cout<<s<<" ";
// }while (next_permutation(s.begin(), s.end()));
//
// cout<<endl;
// }
// return 0;
//}
//2
//abc
//abc acb bac bca cab cba
//absg
//abgs absg agbs agsb asbg asgb bags basg bgas bgsa bsag bsga gabs gasb gbas gbsa gsab gsba sabg sagb sbag sbga sgab sgba
#include<bits/stdc++.h>
using namespace std;
void permutation(string s, int l, int r)
{
if(l==r) //base case
cout<<s<<" ";
else{
for(int i=l;i<=r;i++)
{
swap(s[l], s[i]); //swap
permutation(s,l+1,r); //recursive call
swap(s[l],s[i]); //backtracing step
}
}
}
int main()
{
string s;
cin>>s;
permutation(s,0,s.length()-1);
cout<<endl;
return 0;
}
//abc
//abc acb bac bca cba cab