×

Instructions

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.


Undo Typing System - Stack (Difficulty - Easy)


Undo Typing System – Stack Problem

Problem Statement:

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.

Input:

  • operations: A list of strings. Each element is either a lowercase letter ('a'–'z') or the string "UNDO".

Output:

Return the final text in the notepad after all operations.

Constraints:

  • 0 ≤ operations.length ≤ 10⁵
  • Each operation is a lowercase letter or "UNDO"
  • Operations must be processed in the given order
  • "UNDO" on an empty stack should do nothing

Example 1:

input: ["a", "b", "c", "UNDO"]
output: "ab"

Explanation:
Type 'a' → "a"
Type 'b' → "ab"
Type 'c' → "abc"
UNDO → removes 'c' → "ab"
    

Example 2:

input: ["a", "UNDO", "UNDO", "b"]
output: "b"

Explanation:
Type 'a' → "a"
UNDO → ""
UNDO → still empty
Type 'b' → "b"
    

Example 3:

input: []
output: ""

Explanation:
No operations performed.
    

Example 4:

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"
    


Time Left:



Processing...

Error:


                

Output:

Test Case Result Input Correct Output Your Output




Submission Result