Generate list of all possible permutations of a string

后端 未结 30 2549
故里飘歌
故里飘歌 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:33

    Here is a link that describes how to print permutations of a string. http://nipun-linuxtips.blogspot.in/2012/11/print-all-permutations-of-characters-in.html

    0 讨论(0)
  • 2020-11-22 15:35

    I just whipped this up quick in Ruby:

    def perms(x, y, possible_characters)
      all = [""]
      current_array = all.clone
      1.upto(y) { |iteration|
        next_array = []
        current_array.each { |string|
          possible_characters.each { |c|
            value = string + c
            next_array.insert next_array.length, value
            all.insert all.length, value
          }
        }
        current_array = next_array
      }
      all.delete_if { |string| string.length < x }
    end
    

    You might look into language API for built in permutation type functions, and you might be able to write more optimized code, but if the numbers are all that high, I'm not sure there is much of a way around having a lot of results.

    Anyways, the idea behind the code is start with string of length 0, then keep track of all the strings of length Z where Z is the current size in the iteration. Then, go through each string and append each character onto each string. Finally at the end, remove any that were below the x threshold and return the result.

    I didn't test it with potentially meaningless input (null character list, weird values of x and y, etc).

    0 讨论(0)
  • 2020-11-22 15:36

    Some working Java code based on Sarp's answer:

    public class permute {
    
        static void permute(int level, String permuted,
                        boolean used[], String original) {
            int length = original.length();
            if (level == length) {
                System.out.println(permuted);
            } else {
                for (int i = 0; i < length; i++) {
                    if (!used[i]) {
                        used[i] = true;
                        permute(level + 1, permuted + original.charAt(i),
                           used, original);
                        used[i] = false;
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            String s = "hello";
            boolean used[] = {false, false, false, false, false};
            permute(0, "", used, s);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 15:36
    def gen( x,y,list): #to generate all strings inserting y at different positions
    list = []
    list.append( y+x )
    for i in range( len(x) ):
        list.append( func(x,0,i) + y + func(x,i+1,len(x)-1) )
    return list 
    
    def func( x,i,j ): #returns x[i..j]
    z = '' 
    for i in range(i,j+1):
        z = z+x[i]
    return z 
    
    def perm( x , length , list ): #perm function
    if length == 1 : # base case
        list.append( x[len(x)-1] )
        return list 
    else:
        lists = perm( x , length-1 ,list )
        lists_temp = lists #temporarily storing the list 
        lists = []
        for i in range( len(lists_temp) ) :
            list_temp = gen(lists_temp[i],x[length-2],lists)
            lists += list_temp 
        return lists
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-11-22 15:38

    In Perl, if you want to restrict yourself to the lowercase alphabet, you can do this:

    my @result = ("a" .. "zzzz");
    

    This gives all possible strings between 1 and 4 characters using lowercase characters. For uppercase, change "a" to "A" and "zzzz" to "ZZZZ".

    For mixed-case it gets much harder, and probably not doable with one of Perl's builtin operators like that.

    0 讨论(0)
提交回复
热议问题