fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool should_swap(string s,int index,int curr){
  5. for(int i=index;i<curr;i++){
  6. if(s[index]==s[curr]) return false;
  7. }
  8. return true;
  9. }
  10.  
  11. void permute(string s,int index){
  12.  
  13. if(index>=s.length()){
  14. cout<<s<<endl;
  15. return;
  16. }
  17.  
  18. for(int i=index;i<s.length();i++){
  19. if(should_swap(s,index,i)){
  20. swap(s[index],s[i]);
  21. permute(s,index+1);
  22. swap(s[index],s[i]);
  23. }
  24. }
  25. }
  26.  
  27. int main(){
  28.  
  29. string s;
  30. cin>>s;
  31. sort(s.begin(),s.end());
  32. permute(s,0);
  33.  
  34. }
Success #stdin #stdout 0s 15240KB
stdin
aba
stdout
aab
aba
baa