Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //{ Driver Code Starts
- #include<bits/stdc++.h>
- using namespace std;
- // } Driver Code Ends
- class Solution
- {
- public:
- void permute(int l,string &s,set<string> &ans)
- {
- if(l==s.size())
- {
- ans.insert(s);
- return;
- }
- for(int i=l;i<s.size();i++)
- {
- swap(s[i],s[l]);
- permute(l+1,s,ans);
- swap(s[i],s[l]);
- }
- }
- vector<string>find_permutation(string S)
- {
- set<string> ans;
- sort(S.begin(),S.end());
- permute(0,S,ans);
- vector<string> res(ans.begin(),ans.end());
- return res;
- }
- };
- //{ Driver Code Starts.
- int main(){
- int t;
- cin >> t;
- while(t--)
- {
- string S;
- cin >> S;
- Solution ob;
- vector<string> ans = ob.find_permutation(S);
- sort(ans.begin(),ans.end());
- for(auto i: ans)
- {
- cout<<i<<" ";
- }
- cout<<"\n";
- }
- return 0;
- }
- // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment