604 Design Compressed String Iterator
Design and implement a data structure for a compressed string iterator. It should support the following operations: next and hasNext.
The given compressed string will be in the form of each letter followed by a positive integer representing the number of this letter existing in the original uncompressed string.
next() - if the original string still has uncompressed characters, return the next letter; Otherwise return a white space. hasNext() - Judge whether there is any letter needs to be uncompressed.
Note: Please remember to RESET your class variables declared in StringIterator, as static/class variables are persisted across multiple test cases. Please see here for more details.
Example:
The Idea: I took an old C approach using a pointer. The general idea is to maintain an iterator for the compressed string, and only increment it when nessessary. The constructor begins by exposing the current character and the digits that follow. It will always follow that these digits are always one index ahead of the current iterator. Next()
: returns the current character at the iterator and decrements the identified compressed digits. When the digit is 0, it increments to the next character (by adding the length of the previous compressed bit), and then identifies the next set of digits. hasNext()
: Will always return true unless the iterate exceeds the bounds of the string.
Complexity: O(1) time and space
Last updated