- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1286.java
52 lines (45 loc) · 1.73 KB
/
_1286.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
45
46
47
48
49
50
51
52
packagecom.fishercoder.solutions.secondthousand;
importjava.util.ArrayList;
importjava.util.List;
publicclass_1286 {
publicstaticclassSolution1 {
publicstaticclassCombinationIterator {
List<String> list;
intindex;
intcombinationLength;
boolean[] visited;
publicCombinationIterator(Stringcharacters, intcombinationLength) {
this.index = 0;
this.list = newArrayList<>();
this.combinationLength = combinationLength;
this.visited = newboolean[characters.length()];
buildAllCombinations(characters, 0, newStringBuilder(), visited);
}
privatevoidbuildAllCombinations(
Stringcharacters, intstart, StringBuildersb, boolean[] visited) {
if (sb.length() == combinationLength) {
list.add(sb.toString());
return;
} else {
for (inti = start; i < characters.length(); ) {
if (!visited[i]) {
sb.append(characters.charAt(i));
visited[i] = true;
buildAllCombinations(characters, i++, sb, visited);
visited[i - 1] = false;
sb.setLength(sb.length() - 1);
} else {
i++;
}
}
}
}
publicStringnext() {
returnlist.get(index++);
}
publicbooleanhasNext() {
returnindex < list.size();
}
}
}
}