-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDesignCompressedStringIterator.cpp
52 lines (45 loc) · 1.05 KB
/
DesignCompressedStringIterator.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
46
47
48
49
50
51
52
class StringIterator {
public:
string s;
int curr, n, count;
char nxt;
StringIterator(string compressedString) {
s = compressedString;
curr = 0;
n = compressedString.size();
count = 0;
nxt = ' ';
}
int getCount(string s, int& i){
string count = "";
while(i < n && isdigit(s[i])){
count += s[i];
i++;
}
return stoi(count);
}
char next() {
if(hasNext()){
if(count == 0){
nxt = s[curr];
curr++;
count = getCount(s, curr);
}
count--;
return nxt;
}
return ' ';
}
bool hasNext() {
if(curr < n || count > 0){
return true;
}
return false;
}
};
/**
* 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();
*/