← 返回 netflix 的题目列表Command Execution System with Tag-Based Undo
类型:online_judge
Implement a command execution system that records commands in time order and supports undo operations.
You need to implement two functions:
execute(command: str, tags: List[str]) -> None
undo(tag: Optional[str] = None) -> str
Rules:
Each call to execute executes a command command and associates it with a list of tags (a command may have multiple tags). Commands are ordered by execution time (later = more recent).
undo() with no tag undoes the most recently executed command.
undo(tag) with a tag undoes the most recent command that has this tag (scan history from newest to oldest and pick the first command containing that tag).
A command can be undone at most once; once undone it must not be returned by any future undo.
undo(...) returns the command string that was undone.
Design appropriate data structures to support these operations efficiently.
Constraints:
Total number of execute and undo calls up to 1e5
Each command has k tags, where 0 <= k <= 10
command is an opaque string identifier
Example:
execute("A", ["x"])
execute("B", ["y"])
execute("C", ["x","y"])
undo() returns "C"
undo("x") returns "A"
undo("y") returns "B"
Example
Input
execute A x
execute B y
execute C x y
undo
undo x
undo y
Output
C
A
B