Skip to content

Instantly share code, notes, and snippets.

@RitamChakraborty
Created March 30, 2020 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RitamChakraborty/ca8a4f039bd38819963678851aacdf70 to your computer and use it in GitHub Desktop.
Save RitamChakraborty/ca8a4f039bd38819963678851aacdf70 to your computer and use it in GitHub Desktop.
Create different types of combination with a given set of elements.
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Stack;
public class CombinationGenerator {
private void func3(int[] arr, Stack<Integer> stack, ArrayList<String> list, int j) {
if (j < arr.length) {
for (int value : arr) {
stack.push(value);
StringBuilder stringBuilder = new StringBuilder();
stack.forEach(stringBuilder::append);
list.add(stringBuilder.toString());
func3(arr, stack, list, j + 1);
stack.pop();
}
}
}
/**
* Generate combination of all the elements
*
* @param arr elements
*/
private ArrayList<String> generatorType3(int[] arr) {
ArrayList<String> list = new ArrayList<>();
func3(arr, new Stack<Integer>(), list, 0);
return list;
}
private void func2(int[] arr, LinkedHashSet<Integer> set, ArrayList<String> list, int j) {
if (j < arr.length) {
for (int value : arr) {
if (!set.contains(value)) {
set.add(value);
StringBuilder stringBuilder = new StringBuilder();
set.forEach(stringBuilder::append);
list.add(stringBuilder.toString());
func2(arr, set, list, j + 1);
set.remove(value);
}
}
}
}
/**
* Generate combination of all the elements
* but without repetition
*
* @param arr elements
*/
private ArrayList<String> generatorType2(int[] arr) {
ArrayList<String> list = new ArrayList<>();
func2(arr, new LinkedHashSet<>(), list, 0);
return list;
}
private void func1(int[] arr, LinkedHashSet<Integer> set, ArrayList<String> list, int j) {
if (j < arr.length) {
for (int value : arr) {
if (!set.contains(value)) {
set.add(value);
if (j == arr.length - 1) {
StringBuilder stringBuilder = new StringBuilder();
set.forEach(stringBuilder::append);
list.add(stringBuilder.toString());
}
func1(arr, set, list, j + 1);
set.remove(value);
}
}
}
}
/**
* Generate combination of all the elements
* of size equal to the size of elements
*
* @param arr elements
*/
private ArrayList<String> generatorType1(int[] arr) {
ArrayList<String> list = new ArrayList<>();
func1(arr, new LinkedHashSet<>(), list, 0);
return list;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3};
Main main = new Main();
ArrayList<String> list1 = main.generatorType1(arr);
ArrayList<String> list2 = main.generatorType2(arr);
ArrayList<String> list3 = main.generatorType3(arr);
System.out.println("First type of combination..");
list1.forEach(System.out::println);
System.out.println("Size of the list: " + list1.size());
System.out.println("Second type of combination..");
list2.forEach(System.out::println);
System.out.println("Size of the list: " + list2.size());
System.out.println("Third type of combination");
list3.forEach(System.out::println);
System.out.println("Size of the list: " + list3.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment