"The point of the sword" question 28: string sort (cow-guest-net version) Java

Source: Internet
Author: User

The title description enters a string that prints out all the permutations of the characters in the string in the dictionary order. For example, enter the string ABC and print out all the strings abc,acb,bac,bca,cab and CBA that can be arranged by the character A,b,c.

Input Description: Enter a string with a length of not more than 9 (there may be a repetition of characters), and the characters include only uppercase and lowercase letters.

In particular, there are 2 points to note here: 1. All combinations cannot be duplicated, such as entering "AA", then the output should be "AA"; 2. Output results sorted by dictionary order

If you use the method on the point of the sword, it is obvious that the two points are not satisfied, such as input "ABC", the output is: "abc" "ACB" "BAC" "BCA" "CBA" "Cab", if the input "AA", the output is "AA" "AA". Obviously, both of these results are problematic.

Here is the code for the "Offer of Swords" version:

1 Importjava.util.ArrayList;2 Importjava.util.List;3  Public classSolution {4     Privatelist<string> list =NewArraylist<>();5     PrivateStringBuffer buffer;6      PublicArraylist<string>permutation (String str) {7         if(str==NULL|| Str.length () ==0)return(arraylist<string>) List;8Buffer =NewStringBuffer (str);9Permutationcore (0);Ten         return(arraylist<string>) List; One     } A      Public voidPermutationcore (intbegin) { -         if(Begin==buffer.length ()-1){ - List.add (buffer.tostring ()); the         } -          for(inti = Begin;i<buffer.length (); i++){ - swap (begin,i); -Permutationcore (begin+1); + swap (begin,i); -         } +     } A      Public voidSwapintIintj) { at         CharA =Buffer.charat (i); -         Charb =Buffer.charat (j); - Buffer.setcharat (i, b); - Buffer.setcharat (J, a);  -     } -}

So how to solve the above two points exist problem? Here to recommend a data structure, TreeSet. This in C + + I do not know there is no, should be no, but as a Java programmer, this is a relatively happy place.

TreeSet The data structure itself is implemented with a red-black tree that automatically sorts strings in dictionary order, and because it implements the set interface, it also guarantees that all results are a set. And the friends who have learned discrete mathematics know that the elements in the collection are not duplicated, so if we use TreeSet to store the results of the arrangement, we can easily achieve the above requirements. Here is the code I implemented:

1 Importjava.util.ArrayList;2 ImportJava.util.Iterator;3 Importjava.util.List;4 ImportJava.util.Set;5 ImportJava.util.TreeSet;6  Public classSolution {7    Privatelist<string> list =NewArraylist<>();8     PrivateSet<string> set =NewTreeset<>();9     PrivateStringBuffer buffer;Ten      PublicArraylist<string>permutation (String str) { One         if(str==NULL|| Str.length () ==0)return(arraylist<string>) List; ABuffer =NewStringBuffer (str); -Permutationcore (0); -Iterator<string> Iterator =set.iterator (); the          while(Iterator.hasnext ()) { - List.add (Iterator.next ()); -         } -         return(arraylist<string>) List; +     } -      Public voidPermutationcore (intbegin) { +         if(Begin==buffer.length ()-1){ A Set.add (buffer.tostring ()); at         } -          for(inti = Begin;i<buffer.length (); i++){ -             //if (Buffer.charat (i) ==buffer.charat (begin) && begin!=i) continue; - swap (begin,i); -Permutationcore (begin+1); - swap (begin,i); in         } -     } to      Public voidSwapintIintj) { +         CharA =Buffer.charat (i); -         Charb =Buffer.charat (j); the Buffer.setcharat (i, b); * Buffer.setcharat (J, a);  $     }Panax Notoginseng}

Input "abc", Output: "abc" "ACB" "BAC" "BCA" "Cab" "CBA", input "AAA", Output "AAA", obviously meet the requirements.

Finally, let's look at the hierarchy of TreeSet:

Similar structures are arraylist,linkedlist,hashset,hashmap,treemap,priorityqueue,stack. The other important interfaces are: List,set,map,queue,deque,comparator,comparable,itrerator. These are often used when writing data structures and algorithms, especially when you need to be aware of them.

"The point of the sword" question 28: string sort (cow-guest-net version) Java

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.