package com.interview.recursion;

public class Combination {

    private void printArray(int arr[],int pos){
        for(int i=0; i < pos; i++){
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
    
    private void combination(int arr[],int pos,int start,int result[]){
    
        printArray(result,pos);
        for(int i = start ; i < arr.length; i++){
            result[pos] = arr[i];
            combination(arr,pos+1,i+1,result);
        }
    }
    
    public static void main(String args[]){
        Combination c = new Combination();
        int arr[] = {1,2,3,4};
        int result[] = new int[arr.length];
        c.combination(arr, 0, 0, result);
    }
    
}

@@ -27,16 +27,37 @@
 rsion;%0A%0A
+import java.util.*;%0A%0A
 public c
@@ -83,40 +83,1179 @@
 %0A   
- private void printArray(int arr
+public void combination(char input%5B%5D)%7B%0A       Map%3CCharacter, Integer%3E countMap = new TreeMap%3C%3E();%0A       for (char ch : input) %7B%0A           countMap.compute(ch, (key, val) -%3E %7B%0A               if (val == null) %7B%0A                   return 1;%0A               %7D else %7B%0A                   return val + 1;%0A               %7D%0A           %7D);%0A       %7D%0A       char str%5B%5D = new char%5BcountMap.size()%5D;%0A       int count%5B%5D = new int%5BcountMap.size()%5D;%0A       int index = 0;%0A       for (Map.Entry%3CCharacter, Integer%3E entry : countMap.entrySet()) %7B%0A           str%5Bindex%5D = entry.getKey();%0A           count%5Bindex%5D = entry.getValue();%0A           index++;%0A       %7D%0A       char%5B%5D output = new char%5Binput.length%5D;%0A       combination(str, count, 0, output, 0);%0A    %7D%0A%0A    private void combination(char input%5B%5D,int count%5B%5D,int pos, char output%5B%5D,int len)%7B%0A        print(output, len);%0A        for(int i=pos; i %3C input.length; i++)%7B%0A            if (count%5Bi%5D == 0) %7B%0A                continue;%0A            %7D%0A            output%5Blen%5D = input%5Bi%5D;%0A            count%5Bi%5D--;%0A            combination(input, count, i, output, len + 1);%0A            count%5Bi%5D++;%0A        %7D%0A    %7D%0A%0A    private void print(char result
 %5B%5D,i
@@ -1328,19 +1328,22 @@
 t.print(
-arr
+result
 %5Bi%5D + %22 
@@ -1388,28 +1388,195 @@
 ln();%0A    %7D%0A
+%0A
     
+public void combinationEasy(char%5B%5D input) %7B%0A        List%3CCharacter%3E r = new ArrayList%3C%3E();%0A        Arrays.sort(input);%0A        combinationEasiest(input, 0, r);%0A    %7D%0A
 %0A    private
@@ -1596,86 +1596,141 @@
 tion
-(int 
+Easiest(ch
 ar
-r
 %5B%5D
-,int pos,int start,int result%5B%5D)%7B%0A    %0A        printArray(result,pos
+ input, int pos, List%3CCharacter%3E r) %7B%0A%0A        r.forEach(r1 -%3E System.out.print(r1 + %22 %22));%0A        System.out.println(
 );%0A 
@@ -1735,24 +1735,25 @@
 %0A        for
+ 
 (int i = sta
@@ -1753,23 +1753,22 @@
 i = 
-start 
+pos
 ; i %3C 
-arr
+input
 .len
@@ -1768,32 +1768,33 @@
 put.length; i++)
+ 
 %7B%0A            re
@@ -1795,28 +1795,117 @@
     
-result%5Bpos%5D = arr
+if (i != pos && input%5Bi%5D == input%5Bi-1%5D) %7B%0A                continue;%0A            %7D%0A            r.add(input
 %5Bi%5D
+)
 ;%0A  
@@ -1929,29 +1929,67 @@
 tion
-(arr,pos+1,i+1,result
+Easiest(input, i + 1, r);%0A            r.remove(r.size() - 1
 );%0A 
@@ -2003,20 +2003,16 @@
 %7D%0A    %7D%0A
-    
 %0A    pub
@@ -2103,117 +2103,97 @@
     
-int arr%5B%5D = %7B1,2,3,4%7D;%0A        int result%5B%5D = new int%5Barr.length%5D;%0A        c.combination(arr, 0, 0, result
+c.combination(%22aabbc%22.toCharArray());%0A        c.combinationEasy(%22aabbc%22.toCharArray()
 );%0A
+%0A
     

update combination