I am trying to generate the permutations of a string. This code is working fine, but when a char is repeated, a duplicate permutation is printed. Example is given below.
string s;
int num[50], used[50], cur[50];
void permute(int start, int len)
{
if(start == len)
{
for(int i = 0; i < len; i++)
printf("%c", s[num[i]]);
printf("\n");
return;
}
for(int i = 0; i < len; i++)
{
if(used[i] == 0)
{
used[i] = 1;
num[start] = i;
permute(start+1, len);
used[i] = 0;
}
}
}
Input:
aab
Output:
aab
aba
aab
aba
baa
baa