Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Variations {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // On the first line, find the number Z
- int length = scanner.nextInt();
- scanner.nextLine();
- // On the second line, find the symbols X and Y
- String[] input = scanner.nextLine().split(" ");
- String symb1 = input[0];
- String symb2 = input[1];
- String symbols = symb1 + symb2;
- String result = "";
- // Print one variation on each line
- // Ordered lexicographically
- // Print to the standard output
- combine(result, symbols, length);
- }
- private static void combine(String result, String input, int length) {
- if (length == 0) {
- System.out.println(result); // base condition
- } else {
- // for (Character character: input.toCharArray()) {
- // combine(result + character, input, length - 1);
- // }
- for (int i = input.length() - 1; i >= 0; i--) {
- combine(result + input.charAt(i), input, length - 1);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement