Skip to main content

Design Compressed String Iterator

Easy
Data Structures and Algorithms
Editorial
Topics:
Array
String
Design

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.

Need help or want more practice?

Practise in an interviewGet AI Coach help