0

文字列のすべての順列を取得する必要がありますが、ひねりがあります。順列を取得する必要がありますが、長さが異なります。このように: AB の順列は次のようになります。

B

AA

BB

AB

学士

固定長の文字列の順列を取得できますが、これにこだわっています。

4

3 に答える 3

1
public void processPermutations(String s) {
  for (int i=1; i<s.length; i++) {
    String substring = s.substring(0, i);
    // Generate and process the permutations of the substring here...
  }
}
于 2012-09-25T20:28:31.527 に答える
0

あなたはこのようなことをすることができます:

public List<String> getPermutations(String s) {
    List<String> result = new ArrayList<String>();

    if (s.length() > 1) {
        for (int i = 0; i < s.length(); i++) {
            //Create a string that has all the characters of s except the ith one
            String smallerString = s.substring(0,i) + s.substring(i + 1, s.length());
            result.addAll(getPermutations(smallerString));

            //Get permutations involving a single character appearing multiple times,
            //ie. AA, AAA, AAAA, etc.
            String repeatString = new String();
            for (int j = 1; j <= s.length(); j++) {
                repeatString = repeatString + s.charAt(i);
                result.add(repeatString);
            }
        }
    }

    //Add all the permutations using all the string's characters to the list here.
    return result;
}

ただし、その複雑さについては考えないようにしてください;)

于 2012-09-25T20:31:10.857 に答える
0

最初に文字列の組み合わせをバケットの降順で取得し、次に各組み合わせバケットで並べ替える必要があります。たとえば、文字列が 3 文字の場合:

「ABC」の組み合わせで 3 = 1 を選択 「ABC」の組み合わせで 2 = 3 を選択 「ABC」の組み合わせで 1 = 3 を選択

したがって、これら 7 つのケースのそれぞれの順列を見つける必要があります。擬似コード:

int lenString = s.length
int[] all_permutations = new array
for( buckets = 1 to lenString ){
    int[] c = Combinations( s, buckets )
    int[] p = Permutations( c )
    all_permutations += p   
}
于 2012-09-25T21:05:16.020 に答える