class StringIterator
{
public:
vector<pair<char, int>> char_freq;
StringIterator(string compressedString)
{
int n = compressedString.size();
int i = 0;
while (i < n)
{
char c = compressedString[i];
i ++;
int x = 0;
while (i < n && isdigit(compressedString[i]))
{
x = x * 10 + (compressedString[i] - '0');
i ++;
}
char_freq.push_back({c, x});
}
}
char next()
{
if (char_freq.size() == 0)
return ' ';
auto [c, f] = char_freq[0];
if (f == 1)
char_freq.erase(char_freq.begin());
else
char_freq[0].second --;
return c;
}
bool hasNext()
{
return char_freq.size() > 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();
*/
class StringIterator:
def __init__(self, compressedString: str):
self.char_freq = []
n = len(compressedString)
i = 0
while i < n:
c = compressedString[i]
x = 0
i += 1
while i < n and compressedString[i].isdigit():
x = x * 10 + int(compressedString[i])
i += 1
self.char_freq.append([c, x])
def next(self) -> str:
if not self.char_freq:
return " "
c, f = self.char_freq[0]
if f == 1:
self.char_freq.pop(0)
else:
self.char_freq[0][1] -= 1
return c
def hasNext(self) -> bool:
return len(self.char_freq) != 0
# Your StringIterator object will be instantiated and called as such:
# obj = StringIterator(compressedString)
# param_1 = obj.next()
# param_2 = obj.hasNext()