algoblazerEarly Access
LearnProblemsLeaderboard
Log inSign up

All problems

‹ Back to map
1 shown · 0/261 solved

Final String After Undo/Redo

BronzeCommunity Beta
Asked atProphecy
Solve problem →
2000ms256MBAdded Aug 3, 2026Stacks & Queues

You are given an array of strings commands to process in order, starting from an empty text. Each command is one of:

  • "type x" means append the single lowercase letter x to the end of the text. This also clears the redo history.
  • "undo" means remove the last character of the text. If the text is empty, this does nothing.
  • "redo" means re-append the most recently undone character that has not already been reapplied. If there is no such character (nothing was undone, or typing has cleared the redo history), this does nothing.

Return the text after all commands have been processed. The result may be the empty string.

Examples

Example 1

Input: commands = ["type a", "type b", "undo", "redo", "type c"]
Output: "abc"
Explanation: After "type a" and "type b" the text is "ab". "undo" removes 'b' ("a"), "redo" restores it ("ab"), and "type c" appends 'c' ("abc").
Read full statement →

Constraints

  • 1≤1 \leq1≤ commands.length ≤105\leq 10^5≤105
  • Each command is "type x" where x is a single lowercase English letter, "undo", or "redo".

Details

Solved by12 people
Time limit2000ms
Memory256MB
AddedAug 3, 20265 weeks ago
Stacks & Queues

More like this