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 2012-04-12 20:06:42
lol這不是一個創建Android應用的作業。這是隻是程序的一部分,我可以發送你的apk來測試它,但它尚未完成。如果它被回答了,可以請你把鏈接。 – ImGeorge 2012-04-12 20:11:58