Design Compressed String Iterator
|
|
Here’s how this works:
In the
__init__
method, we initializeindex
at 0, which is the starting point in the compressed string.count
is also set to 0, representing the number of occurrences of the current character to be returned, andchar
is the current character to be returned.The
next
method checks whether there is a next character using thehasNext
method. If there is no next character, it returns a whitespace. If thecount
of the current character is zero, it extracts the next character and its count from the compressed string.The
hasNext
method checks whether there are any more characters in the compressed string to be returned. It returns true if there are still characters left to be returned, and false otherwise.