Advertisement
vladimirVenkov

A B Variations length N

Jun 17th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.TreeSet;
  5.  
  6. public class VariationsBrute {
  7.  
  8.     public static void main(String args[]) throws IOException {
  9.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  10.         int number = Integer.parseInt(br.readLine());
  11.  
  12.         String inp = br.readLine();
  13.  
  14.         inp = inp.replace(" ", "");
  15.         brute(inp, number, new StringBuffer());
  16.         tSet.forEach(System.out::println);
  17.     }
  18.     static TreeSet tSet = new TreeSet();
  19.  
  20.     static void brute(String input, int depth, StringBuffer output) {
  21.         if (depth == 0) {
  22.             tSet.add(output.toString());
  23.         } else {
  24.             for (int i = 0; i < input.length(); i++) {
  25.                 output.append(input.charAt(i));
  26.                 brute(input, depth - 1, output);
  27.                 output.deleteCharAt(output.length() - 1);
  28.             }
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement