/**
* 有一个字符串,根据输入参数m,找出字符串的m个字符的所有字符串
例如:
String str ="abc", m=2 得到结果是 "ab" "ac" "bc"
String str ="abcd" , m=3 得到结果是"abc" "acd" "bcd" "abd" 注:程序摘自网上
*
*/
public static void main(String[] args) {
List<String> list = choose("abcde",3);
for (String string : list) {
System.out.println(string);
}
} public static List<String> choose(String target, int m) {
List<String> resultList = new LinkedList<>();
doChoose(resultList, target, "", m, 0);
return resultList;
} private static void doChoose(List<String> resultList, String target,
String resultStr, int m, int head) { // 递归头
if (resultStr.length() == m) {
resultList.add(resultStr);
return;
} // 递归体
for (int i = head; i < target.length(); i++) {
doChoose(resultList, target, resultStr + target.charAt(i), m, i + 1);
}
}