Evaluate the number of Len combinations of strings (Java program)

Source: Internet
Author: User

Import Java. util. list; <br/> Import Java. util. arraylist; <br/>/** <br/> * calculate the number of Len combinations of strings <br/> * @ author wenin819 <br/> * <br/> */<br/> public class combination {<br/>/** <br/> * main method for calculating the number of combinations <br/> */<br/> Public static list <string> combination (string instr, int Len) {<br/> stringbuffer nodoublestr = new stringbuffer (instr); <br/> nodouble (nodoublestr); <br/> If (LEN> = nodoublestr. length () return nu Ll; <br/> return combinationitem (nodoublestr, Len ); <br/>}< br/>/** <br/> * remove duplicate characters <br/> */<br/> Public static void nodouble (stringbuffer input) {<br/> for (INT I = 0; I <input. length (); I ++) {<br/> char temp = input. charat (I); <br/> for (Int J = I + 1; j <input. length ();) {<br/> If (temp = input. charat (j) {<br/> input. deletecharat (j); <br/>} else {<br/> J ++; <br/>}< br/>/** <br/> * Evaluate the number of combinations <br/> */<br/> Public static list <string> combinationitem (stringbuffer input, int Len) {<br/> List <string> rslist = new arraylist <string> (); <br/> Boolean [] array = new Boolean [input. length ()]; <br/> for (INT I = 0; I <array. length; I ++) {<br/> array [I] = false; <br/>}< br/> do {<br/> If (LEN = countoftrue (array) {<br/> stringbuffer temp = new stringbuffer (input ); <br/> for (Int J = temp. length ()-1; J> = 0; j --) {<br/> If (array [J]) continue; <br/> temp. deletecharat (j); <br/>}< br/> rslist. add (new string (temp); <br/>}< br/>}while (booleanadd (array); <br/> return rslist; <br/>}< br/>/** <br/> * calculate the number of Boolean array elements to true <br/> */<br/> Public static int countoftrue (boolean [] array) {<br/> int COUNT = 0; <br/> for (INT I = 0; I <array. length; I ++) {<br/> If (array [I]) Count ++; <br/>}< br/> return count; <br/>}< B R/>/** <br/> * Boolean array auto-addition (same as binary plus 1) <br/> */<br/> Public static Boolean booleanadd (Boolean [] array) {<br/> Boolean flag = true; <br/> for (INT I = 0; I <array. length; I ++) {<br/> If (false = Flag) return true; <br/> If (array. length-1 = I & True = array [I]) {<br/> array [I] = false; <br/> return false; <br/>}< br/> If (false = array [I]) {<br/> array [I] = true; <br/> return true; <br/>}< br/> array [I] = Fals E; <br/>}< br/> return false; <br/>}< br/> Public static void main (string [] ARGs) {<br/> string input = new string ("ABCDE"); <br/> int Len = 3; <br/> If (ARGs. length> 1) {<br/> input = ARGs [0]; <br/> Len = integer. parseint (ARGs [1]); <br/>}< br/> List list = combination (input, Len); <br/> If (list! = NULL) {<br/> system. out. the number of println (input + "+ Len +" combinations is: "+ list. size () + "/n combination numbers:"); <br/> for (INT I = 0; I <list. size (); I ++) {<br/> system. out. println (I + 1 + ":" + list. get (I ). tostring (); <br/>}< br/>

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.