Any combination of algorithm output string characters

Source: Internet
Author: User

Programming lists the full-character combination of a string with no repeating characters in the original string, for example:
The original string is "ABC", and all of the following combinations are printed:
"A" "B" "C"
"AB" "BC" "CA" "BA" "CB" "AC"
"ABC" "ACB" "BAC" "BCA" "Cab" "CBA"

Import Java.util.arraylist;import Java.util.list;public class Test {static char[] chars = "abc". ToCharArray ();p ublic static void Main (string[] args) {for (int i = 0; i < chars.length; i++) {//Get every character list<integer> list = new Array List<integer> (); List.add (i);p lay (list);}} Use recursion to add a character that does not exist in the list each time to the private static void play (List<integer> list) {print (list); for (int i = 0; i < chars.length ; i++) {if (!list.contains (i)) {list<integer> temp = new arraylist<integer> (list), Temp.add (i);p lay (temp);}} Print List contents private static void print (List<integer> list) {for (Integer i:list) System.out.print (Chars[i] + ""); System.out.println ();}}

  

Any combination of algorithm output string characters

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.