K random combinations of N elements in List in Java

Given a List of N Strings, generate and print all possible combinations of R elements in array and return X random combinations from the result. Following is the code for implementing it:

Combinations of a String

Write an algorithm to print all possible combinations of characters in a string.

Since we need to generate combinations, we can start with a single character and then continue to add a character to combinations we have seen so far.

Let’s use “XYZ” as an example.

public void buildTree(String input, StringBuffer output, int k)
    for (int i = k; i < input.length(); i++)
        buildTree(input, output, i + 1);
        output.deleteCharAt(output.length() - 1);

public static void main(String[] args){
     buildTree("XYZ", new StringBuffer(), 0);

Dry Run just to give an idea: