LeetCode 604, Easy. Topics: Design, Array, String, Iterator. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 12 parametrized pytest cases, and a playground notebook:
Problem
Design and implement a data structure for a compressed string iterator. 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.
Implement the StringIterator class:
next() Returns the next character if the original string still has uncompressed characters, otherwise returns a white space.
has_next() Returns true if there is any letter needs to be uncompressed in the original string, otherwise returns false.
Examples
Constraints
1 <= compressedString.length <= 1000
compressedString consists of lower-case an upper-case English letters and digits.
- The number of a single character repetitions in
compressedString is in the range [1, 10^9].
- At most
100 calls will be made to next and hasNext.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026