I haven't written a blog for almost half a month, so I'll write another one today.
String full line I believe everyone is not unfamiliar, for me really is write and forget, forget and write, so decided to be written a blog, nonsense said below I to analyze the problem:
Problem Description: Given a string to write out its full array, such as AB, the whole arrangement is AB,BA, while the ABC of the full array ABC,ACB,BAC,BCA,CAB,CBA.
Problem-Solving ideas: We analyze with specific examples, if ABC, as shown above, its full arrangement is not the string of every character, put in the first position, and then the rest of the string to do the whole arrangement, such as put a in the first place, the remaining BC
The whole arrangement is BC,CB, the combination is ABC,ACB, then put B in the first place, the remaining string arrangement is AC,CA, the combination is, BAC,BCA. Then put all the characters in the string in the first place once, and so the process is all done, then is a string of the full array. So this is a recursive way of thinking, the string is divided into two parts, the first part is taken out of the character, the second part is the remaining characters of the string, the first part of the first place, the second part of the whole arrangement in the second place. Then how to combine the first part and the second part is the key, because the first part, just a character, the second part is the whole arrangement, is a number of strings, my idea is a character accumulation of ideas, we put the first part of the accumulation of a string, the beginning of the string is an empty string, When we take out a character, add the character to the end of the string, and then form a new string, this accumulates the first character, and then takes the accumulated string as a parameter, passing it to the second part to do the whole arrangement of the function, the second part when doing the whole arrangement, Or take out the word multibyte to which the accumulated string, then the second part of the more short, know that can not be torn down, this time the accumulation of strings is actually a string in the whole arrangement, and then the accumulation of the string output is good.
Here's My Code:
Import Java.util.hashset;import Java.util.list;import Java.util.set;public class Test {public static void Getfull ( String str) { if (str==null) { System.out.println ("null"); } else{ getfull (str, "", Str.length (), New hashset<string> ()); } private static void Getfull (String str,string has,int length,set<string> Set) {for (int i=0;i<str.length (); i++) {StringBuilder sb=new StringBuilder (); Char Single=str.charat (i); String front= ""; String behind= "", if (i==0) {front= ""; behind=str.substring (1);} else if (I==str.length ()-1) {front=str.substring (0, Str.length ()-1); behind= "";} Else{front=str.substring (0, I); behind=str.substring (i+1);} Sb.append (front); Sb.append (behind); String temp=has+string.valueof (single), if (Temp.length () ==length) {if (!set.contains (temp)) {Set.add (temp); SYSTEM.OUT.PRINTLN (temp);}} Getfull (Sb.tostring (), Temp,length,set);}} public static void Main (string[] args) {getfull ("abc");}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Full array of strings (Java)