604. Design Compressed String Iterator

Design Compressed String Iterator (LeetCode 604) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.

How to solve Design Compressed String Iterator

Design Compressed String Iterator is a Data Structure Design problem, part of Design Patterns. Design and implement data structures: LRU cache, LFU cache, HashMap, and specialized structures.

Work towards the solution in these steps:

  1. Parse the compressed string into a list of (character, count) pairs for easier iteration.
  2. Keep track of the current character and how many times it should be returned.
  3. When next() is called, return the current character and decrement its count. Move to the next character when the count reaches zero.

The reference solution runs in O(1) amortized time and O(1) space.

The Design Compressed String Iterator solution and editorial walks through the full approach with reference code.

Problem statement

You are given a compressed string compressedString, where each group consists of a single uppercase or lowercase letter followed by a positive integer representing the number of times that letter appears consecutively in the original string. For example, the compressed string "a2b3" represents the uncompressed string "aabbb".

Design a class StringIterator that supports the following operations:

- next(): Returns the next character in the uncompressed string as a string. If there are no more characters to return, returns a single space character " ". - hasNext(): Returns true if there are more characters to be iterated, or false otherwise.

Implement the StringIterator class with the above methods to efficiently iterate through the uncompressed string without decompressing the entire string into memory.

Examples

Example 1

Input: {"compressedString": "a2b3"}

Output: ["a", "a", "b", "b", "b", " "]

Explanation: Iterates through 'aabbb'. After all characters are consumed, returns a space.

Example 2

Input: {"compressedString": "x1y1z1"}

Output: ["x", "y", "z", " "]

Explanation: Each character appears once. Tests single occurrences.

Topics and companies

Topics: Array, String, Design, Iterator.

Reported in interviews at Google, Amazon.

How to practise Design Compressed String Iterator on Thita.ai

Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(1) amortized time and O(1) space.

Open Design Compressed String Iterator in the code editor, or read the Design Compressed String Iterator editorial for a worked solution with its approach and complexity analysis.

Where Design Compressed String Iterator sits in the DSA pattern sheet

Problems related to Design Compressed String Iterator

Other problems that use the same Design (General/Specific) and Design Patterns techniques:

Design Compressed String Iterator FAQ

What is the LeetCode number of Design Compressed String Iterator?

Design Compressed String Iterator is LeetCode problem 604, rated Easy.

What pattern is used to solve Design Compressed String Iterator?

Design Compressed String Iterator is a Data Structure Design problem. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.

What is the time complexity of Design Compressed String Iterator?

The reference solution runs in O(1) amortized time and O(1) space.

Which companies ask Design Compressed String Iterator?

Design Compressed String Iterator has been reported in interviews at Google, Amazon.

Preparing this workspace.