Twitter Timeline Operations Processor
Twitter's timeline is a dynamic feed of content, constantly updated by users' actions. To ensure a consistent and reliable user experience, especially with features like 'undo' or 'revert,' it's crucial to accurately track and process a sequence of user-generated operations. This problem simulates a core mechanism for maintaining the state of a user's visible timeline, reflecting how actions and their reversals impact the displayed content.
Implement the function processTimelineOperations that takes a list of string operations and returns the final state of the user's timeline. Each operation modifies the timeline, which can be thought of as a sequence of tweet IDs. You will need to parse each operation string and apply the corresponding action.
Input:
operations: An array of strings, where each string represents a single operation. Each operation will be one of the following formats:
"TWEET <id>": Adds a new tweet with the given <id> to the end of the timeline."RETWEET <id>": Adds a retweet of an existing tweet with the given <id> to the end of the timeline."UNDO": Reverts the last TWEET or RETWEET operation. If the timeline is empty, this operation does nothing."REVERT_ALL": Clears the entire timeline, removing all tweets.Output:
string[] representing the final state of the timeline. The elements should be tweet IDs, ordered from the oldest (first added) to the newest (last added).<id>) are non-empty strings consisting of alphanumeric characters. You should treat them as opaque strings.UNDO operations only affect the most recent TWEET or RETWEET operation. If the timeline is empty, UNDO has no effect.Examples
Example 1
Input:
operations = ["TWEET 101", "TWEET 102", "UNDO", "RETWEET 201", "TWEET 103"]
Output:
["101","201","103"]
Note:
Example 2
Input:
operations = ["TWEET 500", "REVERT_ALL", "UNDO", "TWEET 501", "RETWEET 600"]
Output:
["501","600"]
Note:
Example 3
Input:
operations = ["UNDO", "TWEET A1", "RETWEET B2", "TWEET C3", "UNDO", "UNDO", "TWEET D4", "REVERT_ALL", "TWEET E5", "RETWEET F6"]
Output:
["E5","F6"]
Note:
Constraints
1 <= operations.length <= 10^5<id>) is a non-empty string, up to 10 characters long, containing only alphanumeric characters (a-z, A-Z, 0-9)."TWEET <id>", "RETWEET <id>", "UNDO", "REVERT_ALL"). No malformed strings will be provided.UNDO and REVERT_ALL operations do not take an <id> parameter.UNDO and REVERT_ALL.Loading editor...