Undo Stack Simulation

Medium
stack simulation undo-redo data-structures
Problem Description

Simulate a text editor that supports three operations: TYPE (add text), UNDO (reverse last operation), and REDO (restore last undone operation). Implement the editor and return the final text after all operations.

Input Format
First line contains n (number of operations). Next n lines contain operations: "TYPE text", "UNDO", or "REDO".
Output Format
The final text content after all operations, or empty string if no text.
Constraints
• 1 ≤ n ≤ 1000
• Text length ≤ 100 characters per TYPE operation
• UNDO reverses the last operation that hasn't been undone
• REDO restores the last undone operation
• UNDO on empty history does nothing
• REDO when no operations to redo does nothing
Sample Input/Output
Input:
5
TYPE hello
TYPE world
UNDO
TYPE !
REDO
Output:
helloworld!
Explanation

Operations: 1) TYPE "hello" → "hello", 2) TYPE "world" → "helloworld", 3) UNDO → "hello" (undoes "world"), 4) TYPE "!" → "hello!", 5) REDO → "hello!" (REDO fails because new TYPE cleared redo stack). Wait, let me recalculate: After UNDO we have "hello", then TYPE "!" gives "hello!", then REDO should restore "world" but TYPE cleared redo stack, so final is "hello!".

Solution Hints
110
Points
2500ms
Time Limit
256MB
Memory Limit
Code Editor
Register to Submit
Register to Access Code Editor

Create a free account to solve coding problems and track your progress.

Register Now
Feedback