-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombinationIterator.cpp
42 lines (32 loc) · 1.04 KB
/
combinationIterator.cpp
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
class CombinationIterator {
public:
queue<string> q;
CombinationIterator(string characters, int combinationLength) {
combinations(characters, 0, "", combinationLength);
}
string next() {
string next = q.front();
q.pop();
return next;
}
bool hasNext() {
return !q.empty();
}
void combinations(string characters, int pos, string soFar, int combinationLength) {
if(soFar.size() == combinationLength) {
q.push(soFar);
return;
}
for(int i = pos; i < characters.size(); ++i) {
soFar.push_back(characters[i]);
combinations(characters, i + 1, soFar, combinationLength);
soFar.pop_back();
}
}
};
/**
* Your CombinationIterator object will be instantiated and called as such:
* CombinationIterator* obj = new CombinationIterator(characters, combinationLength);
* string param_1 = obj->next();
* bool param_2 = obj->hasNext();
*/