Algorithm to get all possible string combinations from array up to certain length

后端 未结 12 957
攒了一身酷
攒了一身酷 2020-12-01 02:22

What is the best algorithm to get all possible string combinations from a given array with a minimum & maximum length value.

Note: This adds complexity since the

12条回答
  •  长情又很酷
    2020-12-01 03:04

    I'd go with recursive solution.
    "Guess" what is the first element, and recurse on the following elements.
    Repeat for all possible guesses.

    pseudo code:

    findCombinations(array, candidate, length):
      //base clause:
      if (candidate.length == length):
           return
      for each i from 0to array.length:
           //i is the next char to add
           candidate.append(array[i])
           //print the candidate, since it is a unique permutation smaller then length:
           print candidate
           //recursively find permutations for the remianing elements
           findCombinations(array,candidate,length)
           //clean up 
           candidate.removeLasElement()
    

    Java Code:

    private static void findCombinations(char[] array, StringBuilder candidate, int length) { 
          //base clause:
          if (candidate.length() == length)
               return;
          for (int i = 0; i < array.length; i++) { 
               //i is the next char to add
               candidate.append(array[i]);
               //print the candidate, since it is a unique permutation smaller then length:
               System.out.println(candidate);
               //recursively find permutations for the remianing elements
               findCombinations(array,candidate,length);
               //clean up 
               candidate.deleteCharAt(candidate.length()-1);
          }
    }
    public static void findCombinations(char[] array,int length) { 
        findCombinations(array, new StringBuilder(), length);
    }
    

    Invoking:

        char[] arr = "abcde".toCharArray();
        findCombinations(arr, 3);
    

    results in:

    a
    aa
    aaa
    aab
    aac
    aad
    aae
    ab
    aba
    abb
    abc
    abd
    abe
    ac
    aca
    acb
    acc
    acd
    ace
    ad
    ada
    adb
    adc
    add
    ade
    ae
    aea
    aeb
    aec
    aed
    aee
    b
    ba
    baa
    bab
    bac
    bad
    bae
    bb
    bba
    bbb
    bbc
    bbd
    bbe
    bc
    bca
    bcb
    bcc
    bcd
    bce
    bd
    bda
    bdb
    bdc
    bdd
    bde
    be
    bea
    beb
    bec
    bed
    bee
    c
    ca
    caa
    cab
    cac
    cad
    cae
    cb
    cba
    cbb
    cbc
    cbd
    cbe
    cc
    cca
    ccb
    ccc
    ccd
    cce
    cd
    cda
    cdb
    cdc
    cdd
    cde
    ce
    cea
    ceb
    cec
    ced
    cee
    d
    da
    daa
    dab
    dac
    dad
    dae
    db
    dba
    dbb
    dbc
    dbd
    dbe
    dc
    dca
    dcb
    dcc
    dcd
    dce
    dd
    dda
    ddb
    ddc
    ffffd
    dde
    de
    dea
    deb
    dec
    ded
    dee
    e
    ea
    eaa
    eab
    eac
    ead
    eae
    eb
    eba
    ebb
    ebc
    ebd
    ebe
    ec
    eca
    ecb
    ecc
    ecd
    ece
    ed
    eda
    edb
    edc
    edd
    ede
    ee
    eea
    eeb
    eec
    eed
    eee
    

提交回复
热议问题