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. The results are output in alphabetical order.
the code is as follows, and Java remains:
Import Java.util.arraylist;import Java.util.collections;import Java.util.hashset;public class Solution {HashSet< string> arrangeset = new hashset<> ();p ublic void Dfsstr (char str[],char sequence[],int step,int StrLen,boolean m Oveflag[]) {if (step = strLen) {String arrange = new String (sequence); Arrangeset.add (arrange); return;} for (int i = 0; i < StrLen; i++) {if (moveflag[i] = = False) {Sequence[step] = str[i];moveflag[i] = true;dfsstr (str, SEQUENC E, step + 1, strLen, Moveflag); moveflag[i] = false; Fallback step}}}public arraylist<string> permutation (String str) {char sequence[] = new Char[str.length ()]; Boolean moveflag[] = new Boolean[str.length ()]; Char strarrary[] = Str.tochararray (); arraylist<string> stringlist = new arraylist<> (); if (str = = NULL | | str.isempty ()) {return stringlist; } for (int i = 0; i < str.length (); i++) {moveflag[i] = false; } dfsstr (StRarrary, sequence, 0, Str.length (), Moveflag); Stringlist.addall (Arrangeset); Collections.sort (stringlist); return stringlist; }}
Recursive programs are the hardest to write ...