给定一个字符串,如String str =“abcd”,输出它所有可能的排列组合。

举例:“abcd”排列组合为

"a","b","c","d","ab","ba","ac","ca","ad","da","bc","cb","bd","db","cd","dc","abc","acb","bac","bca","cab","cba","abd","adb","bda","bad","dab","dba","acd","adc","cda","cad","dac","dca","bcd","bdc","cdb","cbd","dbc","dcb","dabc","dacb","dbac","dbca","dcab","dcba","cabd","cadb","cbda","cbad","cdab","cdba","bacd","badc","bcda","bcad","bdac","bdca","abcd","abdc","acdb","acbd","adbc","adcb"

有这么多种排列组合。

 public void show(int current_recur, String temp)  {
     if(current_recur < str.length()) {
            for(int i = 0; i < str.length(); i++) {
                if( ! ( temp.contains(str.substring(i, i + 1)) ) )  {
                    System.out.println(temp + str.substring(i, i + 1));
                    show(current_recur + 1, new String(temp + str.substring(i, i + 1)));
                }
            }
        }
}

你可能感兴趣的:(给定一个字符串,如String str =“abcd”,输出它所有可能的排列组合。)