Advertisement
nikolstoyneva

Variations

May 30th, 2023 (edited)
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5.  
  6. public class Variations {
  7.     public static void main(String[] args) throws IOException {
  8.         InputStreamReader reader = new InputStreamReader(System.in);
  9.         BufferedReader bf = new BufferedReader(reader);
  10.  
  11.         int length = Integer.parseInt(bf.readLine());
  12.         char[] symbols = bf.readLine().replaceAll(" ", "").toCharArray();
  13.  
  14.         List<String> validCombinations = new ArrayList<>();
  15.  
  16.         getAllValidCombinations("", length, symbols, 0, validCombinations);
  17.  
  18.         Collections.sort(validCombinations);
  19.         for (int i = 0; i < validCombinations.size(); i++) {
  20.             System.out.println(validCombinations.get(i));
  21.         }
  22.     }
  23.  
  24.     private static void getAllValidCombinations(String combination,
  25.                                                 int length,
  26.                                                 char[] symbols,
  27.                                                 int index,
  28.                                                 List<String> validCombinations) {
  29.         if (index == length) {
  30.             validCombinations.add(combination);
  31.             return;
  32.         }
  33.  
  34.         for (int i = 0; i <= 1; i++) {
  35.             String output = combination + symbols[i];
  36.             getAllValidCombinations(output, length, symbols, index + 1, validCombinations);
  37.         }
  38.  
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement