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
]?すべての組み合わせを文字に置換する
私は何を求めているのか理解していただければ幸いです。あなたが理解できない場合、私はさらに説明します。ありがとう、私は感謝します。
宿題? 。それをタグ付けしてください。ちなみに、私はこの質問がここで何度も答えられていると信じていますが、私はあなた自身(これはhwの背後にあるポイントです)でも働こうとすることをお勧めします – Pepe
lolこれは宿題ではありません。 program.iのちょうどのセクションは、それをテストするためにAPKを送ることができますが、まだ完成していません。その答えが出たら、リンクを貼ってください。 – ImGeorge