604. Design Compressed String Iterator
Design an iterator for a run-length encoded string. The encoded string is a sequence of a letter followed by a positive count, for example "L3M2" expands to "LLLMM" and "A2B1C3" expands to "AABCCC".
Implement the StringIterator class:
StringIterator(String compressedString)initializes the iterator with the encoded string.next()returns the next character and advances the iterator. If no characters remain, return a single space" ".hasNext()returnstrueif at least one character remains, otherwisefalse.
Examples
Input: [["StringIterator","hasNext","next","hasNext","next","hasNext","next","hasNext","next","hasNext","next","hasNext","next","hasNext","next","hasNext","next"],[["A2"],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]]
Output: [null,true,"A",true,"A",false," ",false," ",false," ",false," ",false," ",false," "]
Input: [["StringIterator","hasNext","next","hasNext","next","hasNext","next","hasNext","next","hasNext","next","hasNext","next","hasNext","next","hasNext","next","hasNext","next"],[["B2C2"],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]]
Output: [null,true,"B",true,"B",true,"C",true,"C",false," ",false," ",false," ",false," ",false," "]
Hints
Parse each letter together with the digits that follow it; the count may have more than one digit.
Maintain current character and remaining count state.
On each `next()` call, return the current character and decrement count.
Related Problems
604. Design Compressed String Iterator
Design an iterator for a run-length encoded string. The encoded string is a sequence of a letter followed by a positive count, for example `"L3M2"` expands to `"LLLMM"` and `"A2B1C3"` expands to `"AABCCC"`.