-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_PermutationsOfGivenString.cpp
75 lines (61 loc) · 1.19 KB
/
GFG_PermutationsOfGivenString.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
70
71
72
73
74
75
/*
https://practice.geeksforgeeks.org/problems/permutations-of-a-given-string2041/1/#
Permutations of a given string
*/
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
vector<string> ans;
int n;
vector<string>find_permutation(string S)
{
sort(S.begin(), S.end());
do{
ans.push_back(S);
}while(next_permutation(S.begin(), S.end()));
/*
n = S.size();
permute(0,S);
sort(ans.begin(), ans.end());
*/
return ans;
}
void permute(int ind, string &S)
{
if(ind==n-1)
{
ans.push_back(S);
return;
}
for(int k=ind; k<n; k++)
{
if(ind<k && S[k]==S[k-1])continue;
swap(S[ind], S[k]);
permute(ind+1, S);
swap(S[ind], S[k]);
}
}
};
// { Driver Code Starts.
int main(){
int t;
cin >> t;
while(t--)
{
string S;
cin >> S;
Solution ob;
vector<string> ans = ob.find_permutation(S);
for(auto i: ans)
{
cout<<i<<" ";
}
cout<<"\n";
}
return 0;
}
// } Driver Code Ends