Here is the algorithm for printing all permutations ignoring repeats:
char buf[MAX_LEN];
void swap(int i, int j)
{ char t = buf[i]; buf[i] = buf[j]; buf[j] = t; }
void recur(int start, int len)
{
if (len == 0) printf("%s", buf);
else {
for (int i = 0; i < len; ++i) {
swap(start, i);
recur(start + 1, len - 1);
swap(start, i);
}
}
}
void permute(char *s)
{
strcpy(buf, s);
recur(0, strlen(buf));
}
Now the problem arises when swap(start, i); is exchanging two
different copies of the same character. So how can you modify this
algorithm to prevent that?
On Dec 7, 8:01 am, Aniket <[email protected]> wrote:
> Write a programme to produce all permutations of a given string where
> characters are not unique. That means you are not allowed to print the
> duplicate strings.
>
> Ex:
>
> If input is aaa
> The output should be only aaa
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.