-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cpp
39 lines (35 loc) · 924 Bytes
/
Solution.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
class StringIterator {
public:
StringIterator(string compressedString) {
int n = compressedString.size();
int i = 0;
while (i < n) {
char c = compressedString[i];
int x = 0;
while (++i < n && isdigit(compressedString[i])) {
x = x * 10 + (compressedString[i] - '0');
}
d.push_back({c, x});
}
}
char next() {
if (!hasNext()) return ' ';
char ans = d[p].first;
if (--d[p].second == 0) {
++p;
}
return ans;
}
bool hasNext() {
return p < d.size() && d[p].second > 0;
}
private:
vector<pair<char, int>> d;
int p = 0;
};
/**
* Your StringIterator object will be instantiated and called as such:
* StringIterator* obj = new StringIterator(compressedString);
* char param_1 = obj->next();
* bool param_2 = obj->hasNext();
*/