Design Compressed String Iterator - Problem
Design Compressed String Iterator

You need to create a smart iterator for compressed strings! Imagine you have a string like "a3b2c1" which represents the uncompressed string "aaabbc". Your task is to build an iterator that can traverse through the original uncompressed characters one by one, without actually expanding the entire string.

Your StringIterator class should support:
โ€ข next() - Returns the next character in the uncompressed sequence, or a space ' ' if no more characters exist
โ€ข hasNext() - Returns true if there are still characters to iterate through

The compressed format: Each letter is followed by a positive integer representing how many times that letter appears consecutively in the original string.

Example: For compressed string "L1e2t1C1o1d1e1", your iterator should return: 'L', 'e', 'e', 't', 'C', 'o', 'd', 'e' in sequence.

Input & Output

example_1.py โ€” Basic Usage
$ Input: StringIterator("L1e2t1C1o1d1e1")
โ€บ Output: next() calls return: 'L', 'e', 'e', 't', 'C', 'o', 'd', 'e', then ' '
๐Ÿ’ก Note: The compressed string represents 'LeetCode'. Each character is followed by its count. The iterator returns characters one by one in the original sequence.
example_2.py โ€” HasNext Check
$ Input: StringIterator("a3"), check hasNext() before and after exhaustion
โ€บ Output: hasNext(): true, next(): 'a', hasNext(): true, next(): 'a', hasNext(): true, next(): 'a', hasNext(): false
๐Ÿ’ก Note: The hasNext() method correctly identifies when there are more characters to return. After all 3 'a' characters are consumed, hasNext() returns false.
example_3.py โ€” Empty After Completion
$ Input: StringIterator("x1"), call next() twice
โ€บ Output: First next(): 'x', Second next(): ' '
๐Ÿ’ก Note: When the iterator is exhausted and next() is called, it returns a space character ' ' as specified in the problem requirements.

Visualization

Tap to expand
DVD Player IteratorCompressed String: L1e2t1C1o1d1e1Scene DisplayeCurrently PlayingTime Remaining1minutes leftNext Scenet1Ready to loadMemory Usage: O(1)Only stores current scene info, not entire movieโ–ถnext()?hasNext()
Understanding the Visualization
1
Smart Remote Setup
Initialize with compressed string 'L1e2t1C1o1d1e1' - like a DVD with scene list
2
Play Current Scene
Start with first scene: 'L' for 1 minute. Remote tracks current scene and time left
3
Scene Transition
When 'L' scene ends (count=0), automatically load next scene: 'e' for 2 minutes
4
Memory Efficient
Remote only remembers current scene info, not the entire movie - that's O(1) space!
Key Takeaway
๐ŸŽฏ Key Insight: Like a smart DVD remote, we only need to track the current scene and time remaining, not store the entire movie in memory. This makes the iterator both memory-efficient O(1) and fast O(1) per operation!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1) per operation

Each next() and hasNext() call processes at most one character-count pair

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only stores current character, count, and position pointers

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค compressedString.length โ‰ค 1000
  • compressedString consists of lowercase English letters and digits only
  • The number following each character is in the range [1, 109]
  • At most 100 calls will be made to next and hasNext
Asked in
Google 25 Amazon 18 Microsoft 15 Meta 12
28.8K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen