c - All possible combinations of a word -
i in job interview , asked me generate list of possible permutations of given string. solution inefficient , guy interview me told me supposed use recursion. know question?
this classic interview question, solution goes that:
int permu(char* str, size_t len ,size_t index ) { size_t = index - 1; if(index == len) { printf ("%s\n",str); } while (++i < len) { swap (str,index,i); /* swap between index , */ permu(str, len ,index + 1 ); /* recorsion */ swap (str,index,i); /* swap between index , */ } return(0); }
note, in code user should give 0 in index parmether better call function this:
int permutations(char* str, size_t len) { return (permu(str, len ,0)); } static int permu(char* str, size_t len ,size_t index ) { //....}
Comments
Post a Comment