-
Notifications
You must be signed in to change notification settings - Fork 0
/
Permutations.java
44 lines (37 loc) · 1.1 KB
/
Permutations.java
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
35
36
37
38
39
40
41
42
43
44
package com.smlnskgmail.jaman.codewarsjava.kyu4;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
// https://www.codewars.com/kata/5254ca2719453dcc0b00027d
public class Permutations {
private final String input;
public Permutations(String input) {
this.input = input;
}
public List<String> solution() {
List<String> permutations = new LinkedList<>();
permutation("", input, permutations);
return permutations
.stream()
.distinct()
.collect(Collectors.toList());
}
private void permutation(
String prefix,
String string,
List<String> all
) {
int length = string.length();
if (length == 0) {
all.add(prefix);
} else {
for (int i = 0; i < length; i++) {
permutation(
prefix + string.charAt(i),
string.substring(0, i) + string.substring(i + 1, length),
all
);
}
}
}
}