This blog will soon be merged with

JavaByPatel

which explains each solution in detail, to visit new blog, click JavaByPatel

Monday, 25 May 2015

Print all possible strings of length k that can be formed from a set of n characters.


package backtracking;

import java.util.Arrays;

/*
  Find all n length Combinations of a given alphabets.

 First lets see, what does this question means.
 if given n=2 and arr={"A","B"};
 Output: 
  AA
  AB
  BA
  BB

 if given n=2 and arr={"A","B", C};
 Output: 
  AA
  AB
  AC
  BA
  BB
  BC
  CA
  CB
  CC
 and so on.

 How many combinations can be created depends on k^r where k is length of unique Alphabets to use. r is number of places to fill.
 In first example above, k=2(A,B), r=2(need 2 digit combinations)
 2^2 = 4 combinations possible.

 In second example above, k=3(A,B,C), r=2(need 2 digit combinations)
 3^2 = 9 combinations possible.

 How to find it
 ----------------
 If we want to find out 3 digits combination of {A,B,C} ie k=3 and r=3 then 3^3 = 27 combinations possible.

 Logically our loop will flow like below recursively,
 our base case to break is when iteration matches the number of combinations ie n.

 A ->  A  ->   A -> print str and return 
               B -> print str and return 
               C -> print str and return 
       B  ->   A -> print str and return 
               B -> print str and return 
               C -> print str and return 
       C  ->   A -> print str and return 
               B -> print str and return 
               C -> print str and return 
 B ->  A  ->   A -> print str and return 
               B -> print str and return 
               C -> print str and return 
       B  ->   A -> print str and return 
               B -> print str and return 
               C -> print str and return 
       C  ->   A -> print str and return 
               B -> print str and return 
               C -> print str and return 
 C  -> A  ->   A -> print str and return 
               B -> print str and return 
               C -> print str and return 
       B  ->   A -> print str and return 
               B -> print str and return 
               C -> print str and return 
       C  ->   A -> print str and return 
               B -> print str and return 
               C -> print str and return 
 Loop End.   
 */

public class GenerateAllCombinationOfNBits {


 public static void main(String[] args) {

  String[] sarray = {"A","B", "C"};

  new GenerateAllCombinationOfNBits().printCombinationWithoutTemporaryArrayOfBinaryNumbers(new int[2], 2);
  new GenerateAllCombinationOfNBits().printCombinationWithTemporaryArray(sarray, new String[2], 2);
  new GenerateAllCombinationOfNBits().printCombinationWithoutTemporaryArray(sarray,"",2);
 }

 //IF YOU WANT TO FIND COMBINATION OF BINARY DIGITS ONLY ie 0 and 1
 private void printCombinationWithoutTemporaryArrayOfBinaryNumbers(int[] array, int N){
  if (N < 1) {
   for (int i = 0; i < array.length; i++) {
    System.out.print(array[i]);
   }
   System.out.println();
  }
  else  {
   array[N-1]=0;
   printCombinationWithoutTemporaryArrayOfBinaryNumbers(array, N-1);
   array[N-1] = 1;
   printCombinationWithoutTemporaryArrayOfBinaryNumbers(array, N-1);
  }
 }

 public void printCombinationWithoutTemporaryArray(String[] combinationOf, String str, int lengthOfCombination){
  if(lengthOfCombination<1){
   System.out.println(str);
  }else{
   for (int i = 0; i < combinationOf.length; i++) {
    String st = combinationOf[i];
    printCombinationWithoutTemporaryArray(combinationOf, str+st, lengthOfCombination-1);
   }
  } 
 } 

 public void printCombinationWithTemporaryArray(String[] arrayToUse, String[] arrayToWorkOn, int n){
  if(n<1){
   printStringArray(arrayToWorkOn);
  }else{
   for (int i = 0; i < arrayToUse.length; i++) {
    arrayToWorkOn[n-1] = arrayToUse[i];
    printCombinationWithTemporaryArray(arrayToUse, arrayToWorkOn, n-1);
   }
  }
 }

 private static void printStringArray(String[] array2) {
  System.out.println(Arrays.toString(array2));
 }
}



No comments:

Post a Comment