1
public class StringPermutation {    
    public static List<String> getPermutation(String input) {
        List<String> collection = null;   
        if (input.length() == 1) {
            collection = new ArrayList<String>();
            collection.add(input);
            return collection;
        } else {
            collection = getPermutation(input.substring(1));
            Character first = input.charAt(0);
            List<String> result = new ArrayList<String>();
            for (String str : collection) {
                for (int i = 0; i < str.length(); i++) {
                    String item = str.substring(0, i) + first
                            + str.substring(i);
                    result.add(item);
                }
                String item = str.concat(first.toString());
                result.add(item);
            }
            return result;
        }
    }

    public static void main(String[] args) {
        List<String> test = StringPermutation.getPermutation ("CAT");
        System.out.println (test);
    }
}

上記のコードは、指定された文字列を並べ替えます。たとえば、与えられた場合、cat[ ] が返されます。これは優れていますが、文字のサブセット、つまり [ ]cat, act, atc, cta, tca, tacも表示されるようにコードを編集してもらえますか?cat, act, atc, cta, tca, tac] and [at, ta, tc, ca, ac, ct, c, a, t

私が求めていることを理解していただければ幸いです。わからない場合は、詳しく説明しますのでお知らせください。ありがとうございます。

4

1 に答える 1