Generate list of all possible permutations of a string

后端 未结 30 2889
故里飘歌
故里飘歌 2020-11-22 15:10

How would I go about generating a list of all possible permutations of a string between x and y characters in length, containing a variable list of characters.

Any l

30条回答
  •  旧巷少年郎
    2020-11-22 15:37

    ... and here is the C version:

    void permute(const char *s, char *out, int *used, int len, int lev)
    {
        if (len == lev) {
            out[lev] = '\0';
            puts(out);
            return;
        }
    
        int i;
        for (i = 0; i < len; ++i) {
            if (! used[i])
                continue;
    
            used[i] = 1;
            out[lev] = s[i];
            permute(s, out, used, len, lev + 1);
            used[i] = 0;
        }
        return;
    }
    

提交回复
热议问题