classOrderedStream { vector<string> data; int ptr; int n; public: OrderedStream(int _n) { data.resize(_n); ptr = 1; n = _n; } vector<string> insert(int id, string value){ data[id-1] = move(value); vector<string> ans; while (ptr <= n && !data[ptr-1].empty()) { ans.push_back(data[ptr-1]); ++ptr; } return ans; } };
/** * Your OrderedStream object will be instantiated and called as such: * OrderedStream* obj = new OrderedStream(n); * vector<string> param_1 = obj->insert(id,value); */
Time complexity: O(n), space complexity: O(n).
5603. Determine if Two Strings Are Close
Observe the operations for making two strings close. The sufficient and necessary conditions are: the set of characters is the same, and the multiset of character counts is the same.