PackageCom.pb.demo.packclass.demo1;ImportJava.util.HashSet;/*** Programming lists a string of full-character combinations, where there are no repeating characters in the original string, for example: The original string is "abc", printing gets all of the following combinations of conditions "a" "B" "C" "AB" "BC" "CA" "BA" "CB" "BCA" "Cab" "CBA" *@authorDenny * Idea: * 1. Intercept the string, loading one * 2 at a time. Add the truncated prefix to the collection * 3. And the rest of the combination * 4. The length is 1 o'clock, which is added to the collection with the prefix and the remaining combinations * 5. Using recursion **/ Public classDemo9 {StaticHashset<string> set=NewHashset<string>(); Public Static voidMain (string[] args) {String s= "ABC"; GetString (s),""); SYSTEM.OUT.PRINTLN (set); System.out.println (Set.size ()); } Public Static voidgetString (String s,string pre) {if(S.length () ==1) {Set.add (pre+1); return; }Else{ for(inti = 0; I < s.length (); i++) {StringBuilder sb=NewStringBuilder (s); String Str=pre+s.substring (i, i+1); Set.add (str); GetString (Sb.deletecharat (i). toString (), str); } } }}
Results:
[A, AB, ACB, BC, BCA, AC, B, ABC, c, CBA, BAC, Cab, CA, BA, CB]
15
JAVA----Programming lists full-character combinations of a string with no repeating characters in the original string