-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathday-135.cpp
More file actions
65 lines (50 loc) · 1.71 KB
/
day-135.cpp
File metadata and controls
65 lines (50 loc) · 1.71 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Iterator for Combination
Solution
Design an Iterator class, which has:
A constructor that takes a string characters of sorted distinct lowercase
English letters and a number combinationLength as arguments. A function next()
that returns the next combination of length combinationLength in lexicographical
order. A function hasNext() that returns True if and only if there exists a next
combination.
Example:
CombinationIterator iterator = new CombinationIterator("abc", 2); // creates the
iterator.
iterator.next(); // returns "ab"
iterator.hasNext(); // returns true
iterator.next(); // returns "ac"
iterator.hasNext(); // returns true
iterator.next(); // returns "bc"
iterator.hasNext(); // returns false
Constraints:
1 <= combinationLength <= characters.length <= 15
There will be at most 10^4 function calls per test.
It's guaranteed that all calls of the function next are valid.
*/
// Recursive solution to generate combination, and store in queue
// Time complexity O(N*N)
class CombinationIterator {
string text;
queue<string> combinationQue;
public:
void getCombination(int start, int length, string txt) {
if (length == 0) {
combinationQue.push(txt);
return;
}
for (int idx = start; idx <= text.length() - length; idx++) {
getCombination(idx + 1, length - 1, txt + text[idx]);
}
}
CombinationIterator(string characters, int combinationLength) {
text = characters;
string txt = "";
getCombination(0, combinationLength, txt);
}
string next() {
string word = combinationQue.front();
combinationQue.pop();
return word;
}
bool hasNext() { return !combinationQue.empty(); }
};