-
Notifications
You must be signed in to change notification settings - Fork 3
/
604.cpp
45 lines (42 loc) · 1.28 KB
/
604.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
43
44
45
class StringIterator {
public:
string compressedString;
int currentPosition;
int currentCount;
char currentChar;
int n;
StringIterator(string compressedString) {
this->compressedString = compressedString;
currentPosition = 0;
currentCount = 0;
n = compressedString.size();
}
bool isDigit(char c) {
return c >= '0' && c <= '9';
}
char next() {
if (!hasNext()) return ' ';
if (currentCount == 0) {
currentChar = compressedString[currentPosition];
currentPosition++;
int start = currentPosition;
while (currentPosition + 1 < n && isDigit(compressedString[currentPosition + 1])) {
currentPosition++;
}
string temp = compressedString.substr(start, currentPosition - start + 1);
currentCount = stoi(temp);
currentPosition++;
}
currentCount--;
return currentChar;
}
bool hasNext() {
return !((currentCount == 0) && (currentPosition == n));
}
};
/**
* 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();
*/