-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1286-Iterator_for_Combination.java
38 lines (33 loc) · 1.29 KB
/
1286-Iterator_for_Combination.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
/**
* 1. Pre generate all combination through DFS (backtracking)
* 2. Try to record the current index of the combination and find the next without pre generate
*/
class CombinationIterator {
Queue<String> allCombination = new LinkedList<>();
int currentIdx = 0;
public CombinationIterator(String characters, int combinationLength) {
generateAllCombination(0, new StringBuilder(), characters, 0, combinationLength);
}
private void generateAllCombination(int count, StringBuilder current, String characters, int idx, int len) {
if (count == len) {
allCombination.add(current.toString());
return;
}
for (int i = idx; i < characters.length(); i++) {
generateAllCombination(count + 1, current.append(characters.charAt(i)), characters, i + 1, len);
current.deleteCharAt(current.length() - 1);
}
}
public String next() {
return allCombination.poll();
}
public boolean hasNext() {
return !allCombination.isEmpty();
}
}
/**
* Your CombinationIterator object will be instantiated and called as such:
* CombinationIterator obj = new CombinationIterator(characters, combinationLength);
* String param_1 = obj.next();
* boolean param_2 = obj.hasNext();
*/