Print all distinct permutations of a given string with duplicates - cook the code

Sunday, 5 November 2017

Print all distinct permutations of a given string with duplicates

#include<bits/stdc++.h>
using namespace std;
void per(string &s,int count[],int &len,string out=""){
if(out.size()==len){
cout<<out<<endl;
return;
}
for(int i=0;i<s.size();i++){
if(count[i]==0)continue;
out+=s[i];
count[i]--;
per(s,count,len,out);
count[i]++;
out=out.substr(0,out.size()-1);
}
}
int main(){
string s;
cin>>s;
map<char ,int>m;
for(auto x:s){
m[x]++;
}
int count[m.size()];

string str="";
int i=0;
for(auto x:m){
str+=x.first;
count[i]=0;
count[i++]=x.second;
}
i=s.size();
per(str,count,i);

}

No comments:

Post a Comment