jayati

Permutations of a given string

Sep 28th, 2023
1,220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  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(int l,string &s,set<string> &ans)
  10.     {
  11.         if(l==s.size())
  12.         {
  13.             ans.insert(s);
  14.             return;
  15.         }
  16.         for(int i=l;i<s.size();i++)
  17.         {
  18.             swap(s[i],s[l]);
  19.             permute(l+1,s,ans);
  20.             swap(s[i],s[l]);
  21.         }
  22.     }
  23.         vector<string>find_permutation(string S)
  24.         {
  25.           set<string> ans;
  26.           sort(S.begin(),S.end());
  27.           permute(0,S,ans);
  28.           vector<string> res(ans.begin(),ans.end());
  29.           return res;
  30.         }
  31. };
  32.  
  33.  
  34.  
  35. //{ Driver Code Starts.
  36. int main(){
  37.     int t;
  38.     cin >> t;
  39.     while(t--)
  40.     {
  41.         string S;
  42.         cin >> S;
  43.         Solution ob;
  44.         vector<string> ans = ob.find_permutation(S);
  45.         sort(ans.begin(),ans.end());
  46.         for(auto i: ans)
  47.         {
  48.             cout<<i<<" ";
  49.         }
  50.         cout<<"\n";
  51.     }
  52.     return 0;
  53. }
  54.  
  55. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment