-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathWordPermutation.java
More file actions
34 lines (27 loc) · 806 Bytes
/
WordPermutation.java
File metadata and controls
34 lines (27 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package ds_999_misc;
public class WordPermutation {
public static void main(String[] args) {
char[] choices = { 'a','b','c' };
char[] selection = new char[5];
makePermutation(choices, selection);
}
private static void makePermutation(char[] choices, char[] selection) {
makePermutation(choices, selection, 0);
}
private static void makePermutation(char[] choices, char[] selection, int index) {
if(index == selection.length) {
printArray(selection);
} else {
for(int i = 0; i < choices.length; i++) {
selection[index] = choices[i];
makePermutation(choices, selection, index+1);
}
}
}
private static void printArray(char[] selection) {
for(int i = 0; i < selection.length; i++) {
System.out.printf("%c", selection[i]);
}
System.out.print("\n");
}
}