Java implements abc string arrangement and combination, and javaabc arrangement and combination

Source: Internet
Author: User

Java implements abc string arrangement and combination, and javaabc arrangement and combination

1. Repeatable arrangement: a total of 27 types of string consisting of three characters including abc, aaa, aab, aac... ccc

Using recursive thinking, you can select one or three options from abc for the first character, and then convert the problem into a situation where abc is a string of 2 characters, all possibilities can be obtained after recursive loops. Control the loop exit condition.

Recursion can be used to process general-purpose processing without knowing the character length. If you know the length, you only need to use multi-layer loops and draw a conclusion.

public class Permutation {  public static void main(String[] args) {   char[] chs = {'a','b','c'};   per(new char[3], chs, 3-1);  }  public static void per(char[] buf, char[] chs, int len){   if(len == -1){    for(int i=buf.length-1; i>=0; --i)     System.out.print(buf[i]);    System.out.println();    return;   }   for(int i=0; i<chs.length; i++){    buf[len] = chs[i];    per(buf, chs, len-1);   }  } } 

Repeatable. There are 27 cases in total, as shown in the result.

2. Full arrangement: it must contain three characters (abc), which means the characters cannot be duplicated. Last 3*2 = 6 results

You can use the method in 1 to determine whether or not the three characters are equal or not. If they are not equal, it is one of the required full permutation. The time complexity is n ^ n, and the type of the full arrangement is n! So we need to design a type of n! .

You can also use recursion. There are n options for the first string, and the rest is a recursive problem of N-1 scale. The n types of the first character are all strings. Therefore, we can use the first character to exchange with the position 1-n to obtain the n condition, and then recursively process the n-1 scale, but after processing it, we need to change it back, the original character.

public class Arrange {  public static void main(String[] args) {   char[] chs = {'a','b','c'};   arrange(chs, 0, chs.length);  }  public static void arrange(char[] chs, int start, int len){   if(start == len-1){    for(int i=0; i<chs.length; ++i)     System.out.print(chs[i]);    System.out.println();    return;   }   for(int i=start; i<len; i++){    char temp = chs[start];    chs[start] = chs[i];    chs[i] = temp;    arrange(chs, start+1, len);    temp = chs[start];    chs[start] = chs[i];    chs[i] = temp;   }  } } 

Shows the running result. There are 6 combinations.

3. Combination: all three characters of abc

The question of determining whether to select all combinations, that is, whether to select each bit of abc. The first two are possible, and the second two are possible... Therefore, there are 2 ^ n types. 0 indicates not to take, 1 indicates to select, so that AB can be expressed in the form of 110. The expression of abc ranges from 0 to 2 ^ 3-1. Then, the bitwise operation is performed. If the result is 1, the current bitwise is output, and the result 0 is not output.

Public class Comb {public static void main (String [] args) {char [] chs = {'A', 'B', 'C'}; comb (chs );} public static void comb (char [] chs) {int len = chs. length; int nbits = 1 <len; for (int I = 0; I <nbits; ++ I) {int t; for (int j = 0; j <len; j ++) {t = 1 <j; if (t & I )! = 0) {// It is equivalent to the operation. If it is the same as 1, it will be 1 System. out. print (chs [j]);} System. out. println ();}}}

The output result is as follows. The first row is null, indicating that none of them are obtained.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.