All sorts of output strings

Source: Internet
Author: User

All sorts of output strings

Question: The terminal randomly enters a string and outputs all the arrays of the string.

For example, input: "abc", output: abc, acb, bac, bca, cab, CBA

Recursive solution:

The key lies in recursive conditions, boundary conditions (start = end), and deduplication (for example, "aa", which can be de-duplicated using HashSet)

Keywords: permutation[P packet loss mj comment 'TE packet loss (LEAD) n]Arrange and replace recursion[R comment 'K Comment comment (comment) n]Recursion, loop

See the following code:

1 import java. util. arrayList; 2 import java. util. hashSet; 3 import java. util. topology; 4 5 public class RecursionPermutation {6 public static ArrayList <String> result = new ArrayList <String> (); 7 public static HashSet <String> set = new HashSet <String> (); 8/** 9 * main function entry 10 * @ param args11 */12 public static void main (String [] args) {13 System. out. println ("enter a String at will:"); 14 String inputString = new partition (System. in ). next (). toString (); 15 ArrayList <String> result = Permutation (inputString); 16 for (int I = 0; I <result. size (); I ++) {17 System. out. println (result. get (I )); 18} 19} 20 21/** 22 * main entrance to the recursive function 23 * @ param str24 * @ return25 */26 public static ArrayList <String> Permutation (String str) {27 if (str = null | str. length () = 0) 28 return result; 29 else30 Permutation (str, 0, str. length ()-1); 31 result. addAll (set); 32 return result; 33} 34 35/** 36 * recursive condition function 37 * @ param str38 * @ param start39 * @ param end40 */41 public static void Permutation (String str, int start, int end) {42 char [] array = str. toCharArray (); 43 String r = null; 44 if (start = end) {45 r = String. valueOf (array); 46 set. add (r); 47} else {48 for (int I = start; I <= end; I ++) {49 char tmp = array [start]; 50 array [start] = array [I]; 51 array [I] = tmp; 52 53 Permutation (String. valueOf (array), start + 1, array. length-1); 54 55 tmp = array [start]; 56 array [start] = array [I]; 57 array [I] = tmp; 58} 59} 60} 61

The above code has passed the test and verification on the local machine. It is absolutely correct !!!

  

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.