Skip to content

Commit b63199f

Browse files
Added solution for iterator for combination
1 parent 84b8b4f commit b63199f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

iterator_for_combination.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class CombinationIterator {
2+
string s;
3+
queue<string> q;
4+
5+
void getCombination(int start, int length, string txt){
6+
if(length == 0){
7+
q.push(txt);
8+
return;
9+
}
10+
11+
for(int i = start; i <= s.length() - length; ++i)
12+
getCombination(i+1, length-1, txt + s[i]);
13+
}
14+
public:
15+
CombinationIterator(string characters, int combinationLength) {
16+
s = characters;
17+
string txt = "";
18+
getCombination(0, combinationLength, txt);
19+
}
20+
21+
string next() {
22+
string str = q.front();
23+
q.pop();
24+
return str;
25+
}
26+
27+
bool hasNext() {
28+
return !q.empty();
29+
}
30+
};

0 commit comments

Comments
 (0)