Recursive programs are the hardest to write ...

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. 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 ...

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.