JAVA----Programming lists full-character combinations of a string with no repeating characters in the original string

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.