Java interview questions: String Sorting Algorithm

Source: Internet
Author: User
Tags first string

Question: How many combinations can a maximum of 16 of the five ABCDE balls be extracted repeatedly?

For example, there are five combinations of a B c d e which can be composed of one ball, A combination of 5 + 4 + 3 + 2 + 1 = 15 (repeated arrangement of BA and AB is considered as one)

Aa AB ac ad AE

Bb bc bd be

CC CD CE

Dd de

EE

The combination of three balls is (5 + 4 + 3 + 2 + 1) + (4 + 3 + 2 + 1) + (3 + 2 + 1) + (2 + 1) + 1 = 35

Aaa aab aac aad AAE

Abb abc Abd Abe

Acc acd ace

Add Ade

Aee

 

Bbb bbc bbd bbe

BCC BCD BCE

BDD BDE

Bee

 

CCC CCD CCE

CDD CDE

CEE

 

Ddd DDE

Dee

 

Eee

 

CodeAs follows:

/*
* It is mainly implemented by recursion. The main idea is to divide a string into two segments for processing. First, extract the first string and use the following characters to piece it together.
*/
Import java. util. collections;

Public class zuhe {
Private Static string STR = "ABCDE"; // string
Private Static int n = 3; // number of options
Private Static int COUNT = 0; // number of combinations

Public static void main (string [] ARGs ){
New zuhe ();
}

Zuhe (){
Wrote input = new partition (system. In );
System. Out. println ("Enter the number to be selected (less than" + Str. Length () + )");
N = integer. parseint (input. nextline ());

Find ("", 0 );
System. Out. println ("Total" + Count + "Combination ");

}
/*
* The first parameter indicates the first character, and the second parameter indicates the position of the Start vertex.
*/
Public static void find (string S, int I ){
// Save the last string
String temp = s;
// Determine whether the request meets the requirements
If (S. Length () = N ){
Count ++;

System. Out. Print (S + "");
If (count % 10 = 0)
System. Out. println ();
Return;
}
// Starts from the search point to the wind condition
For (int K = I; k <Str. Length (); k ++ ){
S = temp;
S + = Str. charat (k );
Find (S, k );
}

}

}

The output result is as follows:

enter the number to be selected (less than 5)
3
aaa aab aac aad aae abb abc Abd Abe ACC
ACD ace add Ade aee bbb bbc bbd bbe BCC
bcd bce bdd bde bee CCC CCD cce cdd cde
cee ddd dde Dee EEE has 35 combinations

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.