Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.telerikacademy.oop;
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- Scanner reader = new Scanner(System.in);
- Random newRandom = new Random();
- Set<String> newSet = new TreeSet<>();
- int partitionSize = Integer.parseInt(reader.nextLine());
- String[] input = reader.nextLine().split(" ");
- int possibleCombinations = (int)Math.pow(2,partitionSize);
- permutate(newSet,possibleCombinations,input,newRandom,partitionSize);
- for (String elem:
- newSet) {
- System.out.println(elem);
- }
- }
- public static void permutate(Set<String> newSet, int possibleCombinations, String[] input, Random rand,int partitionSize){
- StringBuffer stringBuffer = new StringBuffer();
- if(newSet.size() == possibleCombinations){
- return;
- }
- for (int i = 0; i < partitionSize; i++) {
- stringBuffer.append(input[rand.nextInt(input.length)]);
- }
- newSet.add(String.valueOf(stringBuffer));
- permutate(newSet,possibleCombinations,input,rand,partitionSize);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment