/*
    Combinations.java - a class to calculate all combinations of a string of varying lengths
    Copyright (C) 2009, Viren Kumar

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

import java.util.*;

public class Combinations {
    
    public ArrayList<String> getCombos(String input, int k){
        
      //base case of requested combination length reached
        if (k == 0){
            ArrayList<String> result = new ArrayList<String>();
            result.add("");
            return result;
        }
        
        //base case of empty string
        if (input.isEmpty())
            return new ArrayList<String>();
        
        ArrayList<String> finalResult = new ArrayList<String>();
        
        //take first character
        String prefix = new StringBuilder().append((input.charAt(0))).toString();
        
        //delete first character from string now
        String restOfString = input.substring(1);
        
        //get all combinations of length k-1
        ArrayList<String> intermediate = getCombos(restOfString,k-1);
        
        //add prefix back to all combinations of k-1 length
        for(String s : intermediate){
            s = (prefix + s);
            finalResult.add(s);
        }
        
        //now get rest of combinations of length k
        ArrayList<String> tailResult = getCombos(restOfString, k);
        
        //add all to the final result
        finalResult.addAll(tailResult);
        
        return finalResult;
        
    }
}
