Skip to content

Commit 53340e2

Browse files
committed
Permutations of a given string
1 parent 64f17af commit 53340e2

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// { Driver Code Starts
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
// } Driver Code Ends
6+
class Solution
7+
{
8+
public:
9+
void permute(string &S, int n, vector<string> &v)
10+
{
11+
if(n == S.length() - 1)
12+
{
13+
v.push_back(S);
14+
return;
15+
}
16+
for(int i = n ; i < S.length() ; i++)
17+
{
18+
swap(S[n], S[i]);
19+
permute(S, n+1, v);
20+
swap(S[n], S[i]);
21+
}
22+
return;
23+
}
24+
vector<string>find_permutation(string S)
25+
{
26+
// Code here there
27+
vector<string> v;
28+
permute(S, 0, v);
29+
sort(v.begin(), v.end());
30+
return v;
31+
}
32+
};
33+
34+
35+
36+
// { Driver Code Starts.
37+
int main(){
38+
int t;
39+
cin >> t;
40+
while(t--)
41+
{
42+
string S;
43+
cin >> S;
44+
Solution ob;
45+
vector<string> ans = ob.find_permutation(S);
46+
for(auto i: ans)
47+
{
48+
cout<<i<<" ";
49+
}
50+
cout<<"\n";
51+
}
52+
return 0;
53+
}
54+
// } Driver Code Ends

Some_More_Strings/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,10 @@
5151
dnh
5252
12345
5353
Explanation: "dnh" is the url for id 12345
54+
55+
## 6. Permutations of a given string:
56+
Given a string S. The task is to print all permutations of a given string.
57+
Example 1:
58+
Input: ABC
59+
Output: ABC ACB BAC BCA CAB CBA
60+
Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA .

0 commit comments

Comments
 (0)