NewProject Coding is here — build features and fix bugs in real codebases with an AI assistantSee what's new

(This question is a variation of the LeetCode question 158. Read N Characters Given read4 II - Call Multiple Times. If you haven't completed that question yet, it is recommended to solve it first.)

You are given a set of read-only Stream objects where each stream has a read(int n) method that returns up to n characters from the remaining part of the stream. If n is greater than the remaining characters, return all remaining characters. If the remaining is empty, return "".

For example, Stream stream = Stream("01234"), then

  • stream.read(2) will return "01"
  • stream.read(4) will return "234"
  • stream.read(2) will return ""
Stdout
-