C++ faster than 95%, with explanation
Basic Idea\nAs we are given the total number of strings to be inserted, we can use an array of strings of length n to save them, according to their index (which
## Basic Idea\nAs we are given the total number of strings to be inserted, we can use an array of strings of length n to save them, according to their index (which is idKey -1). We also keep track of the next index to start the chunk building from, i.e the next unpublished string in the stream.\n\nThe stream is initialized to n empty strings, (as sentinels), and next is set to 0\nFor each insert call, we first put the given string at its position. Then we traverse the array from the position next, uptil we find another empty string and keep on adding them to the chunk, this way we find the largest possible contiguous chunk starting from the position next. Then we update next to the new empty position, and continue.\n\n## Code\n```\nclass OrderedStream {\n    vector<string> stream;\n    int next;\n    int l;\npublic:\n    OrderedStream(int n) {\n        stream = vector<string>(n, "");\n        next = 0;\n        l = n;\n    }\n    vector<string> insert(int idKey, string value) {\n        vector<string> chunk;\n        stream[idKey-1] = value;\n        if(idKey == next + 1) {\n            //we found the next string in the stream\n            while(next < l && stream[next] != "") {\n                chunk.push_back(stream[next]);\n                next++;\n            }\n        }\n        return chunk;\n    }\n};\n```\n\n## Complexity\n**Time:** **O(n)**, as in all cases, the sum of the lengths of all the chunks will be exactly n, and the runtime of the function depends on the size of the current chunk.\n**Space: O(n)**, as an array of n strings is required to store the stream itself, and each chunk is stored separately, whose lengths add upto n.\n
['C']