-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_StringPermutations.cpp
101 lines (80 loc) · 1.7 KB
/
GFG_StringPermutations.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
https://practice.geeksforgeeks.org/problems/permutations-of-a-given-string-1587115620/1#
String Permutations
https://leetcode.com/problems/permutations/
46. Permutations
*/
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
//Complete this function
vector<string> ans;
int high_ind = 0;
vector<string> permutation(string S)
{
high_ind=S.length()-1;
permute(0, S);
sort(ans.begin(),ans.end());
return ans;
}
void permute(int ind, string& s)
{
if(ind==high_ind)
{
ans.push_back(s);
return ;
}
for(int i=ind; i<=high_ind; i++)
{
swap(s[i],s[ind]);
permute(ind+1, s);
swap(s[i], s[ind]);
}
}
};
// { Driver Code Starts.
int main()
{
int T;
cin>>T;
while(T--)
{
string S;
cin>>S;
Solution ob;
vector<string> vec = ob.permutation(S);
for(string s : vec){
cout<<s<<" ";
}
cout<<endl;
}
return 0;
} // } Driver Code Ends
//LC
class Solution {
public:
vector<vector<int>> ans;
int high_ind;
vector<vector<int>> permute(vector<int>& nums) {
high_ind = nums.size()-1;
find(0, nums);
return ans;
}// end
void find(int ind, vector<int>& nums)
{
if(ind==high_ind)
{
ans.push_back(nums);
return;
}
for(int i=ind; i<=high_ind; i++)
{
swap(nums[i],nums[ind]);
find(ind+1, nums);
swap(nums[i],nums[ind]);
}
}
};