File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Problem: Print all permutations of string
2
+
3
+ // Input: ABC
4
+ // Output: ABC ACB BAC BCA CBA CAB
5
+
6
+ // Concept: Backtracking
7
+
8
+ #include < bits/stdc++.h>
9
+ #define fl (i,a,b ) for (i=a;i<b;i++)
10
+ #define fast ios_base::sync_with_stdio (false );cin.tie(NULL );cout.tie(NULL );
11
+ using namespace std ;
12
+ typedef long long int ll;
13
+ vector<string> permutations;
14
+
15
+ vector<string> permute (string str, ll l, ll r)
16
+ {
17
+ if (l==r)
18
+ {
19
+ permutations.push_back (str);
20
+ return permutations;
21
+ }
22
+ for (ll i=l; i<=r; i++)
23
+ {
24
+ swap (str[l], str[i]);
25
+ permute (str, l+1 , r);
26
+ swap (str[l], str[i]);
27
+ }
28
+ return permutations;
29
+ }
30
+
31
+ int main ()
32
+ {
33
+ fast;
34
+
35
+ string str;
36
+ cin>>str;
37
+
38
+ vector <string> ans= permute (str, 0 , str.length ()-1 );
39
+
40
+ for (auto x: ans)
41
+ cout<<x<<" " ;
42
+
43
+ return 0 ;
44
+
45
+ }
You can’t perform that action at this time.
0 commit comments