Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. //checking is current element matches with any element then it will
  6. //produce redundant results
  7. bool checkif(string &in,int i,int j){
  8. for(int k=i;k<j;k++){
  9. if(in[k] == in[j])
  10. return 0;
  11. }
  12. return 1;
  13. }
  14.  
  15. void distinctPermutations(string &in,int i){
  16. //Base case
  17.  
  18. if(in[i]==NULL){
  19. cout<<in<<endl;
  20. return;
  21. }
  22.  
  23. //Recursive case
  24.  
  25. bool flag=0;
  26. for(int j=i;in[j]!=NULL;j++){
  27.  
  28. bool check = checkif(in,i,j);
  29.  
  30. if(check) {
  31. swap(in[j],in[i]);
  32. distinctPermutations(in,i+1);
  33. swap(in[j],in[i]);
  34. }
  35.  
  36. }
  37. }
  38.  
  39. int main() {
  40. // your code goes here
  41. string in;
  42. cin>>in;
  43. sort(in.begin(),in.end());
  44. distinctPermutations(in,0);
  45. return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement