Advertisement
ledkaa

Variations

May 29th, 2023
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class Variations {
  5. public static void main(String[] args) {
  6.  
  7. Scanner scanner = new Scanner(System.in);
  8. // On the first line, find the number Z
  9. int length = scanner.nextInt();
  10. scanner.nextLine();
  11. // On the second line, find the symbols X and Y
  12. String[] input = scanner.nextLine().split(" ");
  13. String symb1 = input[0];
  14. String symb2 = input[1];
  15. String symbols = symb1 + symb2;
  16.  
  17. String result = "";
  18. // Print one variation on each line
  19. // Ordered lexicographically
  20. // Print to the standard output
  21. combine(result, symbols, length);
  22. }
  23.  
  24. private static void combine(String result, String input, int length) {
  25. if (length == 0) {
  26. System.out.println(result); // base condition
  27. } else {
  28. // for (Character character: input.toCharArray()) {
  29. // combine(result + character, input, length - 1);
  30. // }
  31. for (int i = input.length() - 1; i >= 0; i--) {
  32. combine(result + input.charAt(i), input, length - 1);
  33. }
  34.  
  35. }
  36. }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement