-
Notifications
You must be signed in to change notification settings - Fork 0
/
IteratorforCombination.java
34 lines (27 loc) · 1.04 KB
/
IteratorforCombination.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
public class IteratorforCombination {
/**
* Explanation : https://leetcode.com/problems/iterator-for-combination/discuss/789455/Java-Algorithm-Explained-or-Queue-Generate-all-Combinations-of-CombinationLength
*/
class CombinationIterator {
private Queue<String> queue;
public CombinationIterator(String characters, int combinationLength) {
this.queue = new LinkedList<>();
combinations(characters, 0, "", combinationLength, queue);
}
public void combinations(String characters, int start, String soFar, int k, Queue<String> queue) {
if (k == 0) {
queue.add(soFar);
return;
}
for (int i = start; i < characters.length(); i++) {
combinations(characters, i + 1, soFar + characters.charAt(i), k - 1, queue);
}
}
public String next() {
return this.queue.poll();
}
public boolean hasNext() {
return !this.queue.isEmpty();
}
}
}