Wednesday 21 January 2015

Generate Combination in java

public class Combination {
     void doCombination(char []str,int start,int end){   
     char []data=new char[3];
     int i=0;
     int r=3;// how many element combine in one selection
     doCombinationUntil(str,data,start,end,i,r);
     }
    
   void   doCombinationUntil(char []str,char []data,int start,int end,int index,int r){
        if (index==r) {
          for (int j = 0; j < r; j++) {
              System.out.print(data[j]);
          }
          System.out.println("");
          return;
     }
        for (int i=start; i<=end && end-i+1 >= r-index; i++)
         {
             data[index] = str[i];
             doCombinationUntil(str, data, i+1, end, index+1, r);
         }
     }
    
     public static void main(String[] args) {  
          String str1="abcd";
          char []str=str1.toCharArray();
          Combination obj=new Combination();
          obj.doCombination(str,0,(str.length-1));
     }
    

}



 Related Posts:


Check a string value is Integer or not in java. (Solution).

Check ia string is hexadecimal number or not (Solution).

Generate Combination in java (Solution).

Generate permutation of string(Solution).

Reverse string in java (Solution).

Find duplicate characters with occurrences in a string(Solution).

Permutation of a number(Soluion).

Split the String in java(Solution).

Convert string into number in java(Solution).

Swap two strings without using any variable(Solution).

Get a number from a string in java(Solution).

Add Two Big Number In Java(Solution).

Expand String(Solution).

Reverse String (Solution).



No comments:

Post a Comment