-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#1286 Iterator for Combination.java
38 lines (35 loc) · 1.1 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
//https://leetcode.com/problems/iterator-for-combination/
class CombinationIterator {
LinkedList<String> q ;
public CombinationIterator(String characters, int combinationLength) {
q = new LinkedList<>();
char[] chars = characters.toCharArray();
helper(chars,combinationLength,new StringBuilder(),0);
}
public void helper(char[] chars, int k, StringBuilder sb, int start) {
if(k == 0) {
q.add(sb.toString());
return;
}
else {
for(int i = start; i < chars.length ; i++) {
int n = sb.length();
sb.append(chars[i]);
helper(chars,k-1,sb,i+1);
sb.setLength(n);
}
}
}
public String next() {
return q.poll();
}
public boolean hasNext() {
return !q.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();
*/