Skip to content

Commit 29c048b

Browse files
Create string_permutations.cpp
1 parent 424cdbb commit 29c048b

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Strings/string_permutations.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
// #ifndef ONLINE_JUDGE
36+
// freopen("input.txt","r",stdin);
37+
// freopen("output.txt","w",stdout);
38+
// #endif
39+
40+
string str;
41+
cin>>str;
42+
43+
vector <string> ans= permute(str, 0, str.length()-1);
44+
45+
for(auto x: ans)
46+
cout<<x<<" ";
47+
48+
return 0;
49+
50+
}

0 commit comments

Comments
 (0)