You just need to make sure that the same character is never swapped to
the same position twice. Here is one way to do it.
#include <stdio.h>
#include <string.h>
void swap(char *s, int i, int j)
{
char t = s[i];
s[i] = s[j];
s[j] = t;
}
void permute(char *s, int n)
{
if (s[n]) {
int i;
unsigned char done[256] = { 0 };
for (i = n; s[i]; i++) {
if (!done[s[i]]) {
swap(s, n, i);
permute(s, n + 1);
swap(s, n, i);
done[s[i]] = 1;
}
}
}
else printf("%s\n", s);
}
int main(int argc, char *argv[])
{
char buf[10 * 1024];
if (argc > 1) {
strcpy(buf, argv[1]);
permute(buf, 0);
}
return 0;
}
On May 7, 6:23 am, Sairam <[email protected]> wrote:
> Thanks for ur clean Code!! But you haven't considered the case of repeating
> characters in a string
--
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.