/* * Print all permutations of string ``s´´. (C++) * * Solved as a counting problem and coded in a hurry. * * A more elegant and recursive solution: * http://nicolas-lara.blogspot.com/2009/01/permutations.html */ #include static const char s[] = "abc"; const int MAX = (sizeof(s)/sizeof(char)) - 1; void print(int counts[MAX]) { for ( int n=0; n= MAX ) { counts[p] = 0; inc(counts, p+1); } } bool zero(int counts[MAX]) { for ( int p=0; p < MAX; ++p ) if ( counts[p] != 0 ) return false; return true; } /* * Are all numbers unique? * * Note that this is a slow O(n^2) algorithm, * but can be improved to O(n). */ bool uniq(int counts[MAX]) { for ( int p=0; p