/**
* 生成一个字符串的所有字符的所有组合,相同字符的不同位置认为是不同的字符
* @param s
*/
public void generateAllPermutations(String s){
for (int i = 0; i < s.length(); i++) {
/**
* String java.lang.String.substring(int beginIndex, int endIndex)
* beginIndex: inclusive
* endIndex: exclusive
*/
/**
* String java.lang.String.substring(int beginIndex)
* if beingIndex == String.length return "" empty String
* if beingIndex > String.length return ERROR:java.lang.StringIndexOutOfBoundsException
*/
ArrayList<String> sub = subPermutations(s.substring(0,i) + s.substring(i + 1));
for (int j = 0; j < sub.size(); j++) {
System.out.println(s.charAt(i) + sub.get(j));
}
}
}
public ArrayList<String> subPermutations(String s){
ArrayList<String> returnValue = new ArrayList<>();
if(0 == s.length() || 1 == s.length()){
returnValue.add(s);
return returnValue;
}
for (int i = 0; i < s.length(); i++) {
ArrayList<String> arrayList = subPermutations(s.substring(0, i) + s.substring(i + 1));
for (int j = 0; j < arrayList.size(); j++) {
returnValue.add(s.charAt(i) + arrayList.get(j));
}
}
return returnValue;
}
Write a method to compute all permutations of a string
最新推荐文章于 2022-05-30 18:56:26 发布