|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.Arrays; |
| 6 | + |
| 7 | +public class Main { |
| 8 | + private static BufferedReader br; |
| 9 | + private static int[] charCount; // 각 문자 a~z의 개수 |
| 10 | + private static char[] inputChars; |
| 11 | + private static char[] output; |
| 12 | + private static int length; |
| 13 | + private static StringBuilder sb; |
| 14 | + |
| 15 | + public static void main(String[] args) throws IOException { |
| 16 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 17 | + int t = Integer.parseInt(br.readLine()); |
| 18 | + sb = new StringBuilder(); |
| 19 | + |
| 20 | + for (int testCase = 0; testCase < t; testCase++) { |
| 21 | + inputChars = br.readLine().toCharArray(); |
| 22 | + Arrays.sort(inputChars); |
| 23 | + length = inputChars.length; |
| 24 | + output = new char[length]; |
| 25 | + charCount = new int[26]; |
| 26 | + |
| 27 | + for (char c : inputChars) { |
| 28 | + charCount[c - 'a']++; |
| 29 | + } |
| 30 | + |
| 31 | + permute(0); |
| 32 | + } |
| 33 | + System.out.print(sb); |
| 34 | + } |
| 35 | + |
| 36 | + private static void permute(int depth) { |
| 37 | + if (depth == length) { |
| 38 | + sb.append(output).append('\n'); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + for (int i = 0; i < 26; i++) { |
| 43 | + if (charCount[i] > 0) { |
| 44 | + output[depth] = (char) ('a' + i); |
| 45 | + charCount[i]--; |
| 46 | + permute(depth + 1); |
| 47 | + charCount[i]++; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
0 commit comments