-
Notifications
You must be signed in to change notification settings - Fork 277
/
CombinationIterator.java
40 lines (33 loc) · 1.09 KB
/
CombinationIterator.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
class CombinationIterator {
PriorityQueue<String> pq = new PriorityQueue<String>();
public CombinationIterator(String characters, int combinationLength) {
generateCombinations(characters, combinationLength, 0, new StringBuilder());
}
private void generateCombinations(String input, int len, int start, StringBuilder currStr){
if(len==0){
pq.add(currStr.toString());
return ;
}
for(int i=start;i<=input.length()-len;i++){
currStr.append(input.charAt(i));
generateCombinations(input, len-1,i+1, currStr);
currStr.setLength(currStr.length()-1);
}
}
public String next() {
if(hasNext()){
return pq.poll();
} else {
return null;
}
}
public boolean hasNext() {
return (pq.size()>0);
}
}
/**
* 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();
*/