Implement Undo and Redo features of a Text Editor

Given an array of strings Q[], consisting of queries of the following types:

Examples:

Input: Q =
Output: AB ABC
Explanation:
Perform “WRITE A” on the document. Therefore, the document contains only “A”.
Perform “WRITE B” on the document. Therefore, the document contains “AB”.
Perform “WRITE C” on the document. Therefore, the document contains “ABC”.
Perform “UNDO” on the document. Therefore, the document contains “AB”.
Print the contents of the document, i.e. “AB”
Perform “REDO” on the document. Therefore, the document contains “ABC”.
Print the contents of the document, i.e. “ABC”

Input: Q =
Output:xz xzy

Approach: The problem can be solved using Stack. Follow the steps below to solve the problem:

Below is the implementation of the above approach: