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
Your StringIterator class should support:
โข
โข
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
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 throughThe 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
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
โ Linear Growth
Space Complexity
O(1)
Only stores current character, count, and position pointers
โ 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
nextandhasNext
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code