Please read the instructions carefully before starting the exam.
Once you start the exam, the timer will begin, and you cannot pause it.
Ensure that you complete and submit your code within the given time if the timer is enabled.
In a notepad typing app, each user keystroke is recorded. Users can either type lowercase letters or press "UNDO"
to remove the last typed letter.
Your task is to simulate the typing process using a stack and return the final text after applying all operations in order.
If an "UNDO"
is attempted on an empty stack, it should be ignored.
operations
: A list of strings. Each element is either a lowercase letter ('a'–'z') or the string "UNDO"
.Return the final text in the notepad after all operations.
0 ≤ operations.length ≤ 10⁵
"UNDO"
"UNDO"
on an empty stack should do nothinginput: ["a", "b", "c", "UNDO"] output: "ab" Explanation: Type 'a' → "a" Type 'b' → "ab" Type 'c' → "abc" UNDO → removes 'c' → "ab"
input: ["a", "UNDO", "UNDO", "b"] output: "b" Explanation: Type 'a' → "a" UNDO → "" UNDO → still empty Type 'b' → "b"
input: [] output: "" Explanation: No operations performed.
input: ["UNDO", "UNDO", "x", "y", "UNDO", "z"] output: "xz" Explanation: UNDO ×2 → ignored Type 'x' → "x" Type 'y' → "xy" UNDO → removes 'y' → "x" Type 'z' → "xz"