Tutorialspoint
Problem
Solution
Submissions

Text Editor with Undo/Redo Functionality

Certification: Intermediate Level Accuracy: 0% Submissions: 0 Points: 15

Design a text editor with the following operations:

Example 1
  • Input: ["TextEditor", "append", "append", "getText", "delete", "getText", "undo", "getText", "redo", "getText"] [[], ["Hello"], [" World"], [], [5], [], [], [], [], []]
  • Output: [null, null, null, "Hello World", null, "Hello", null, "Hello World", null, "Hello"]
  • Explanation:
    • Step 1: TextEditor textEditor = new TextEditor() // Initialize with empty text
    • Step 2: textEditor.append("Hello") // Text = "Hello"
    • Step 3: textEditor.append(" World") // Text = "Hello World"
    • Step 4: textEditor.getText() // Return "Hello World"
    • Step 5: textEditor.delete(5) // Delete the last 5 characters: " World". Text = "Hello"
    • Step 6: textEditor.getText() // Return "Hello"
    • Step 7: textEditor.undo() // Undo the delete operation. Text = "Hello World"
    • Step 8: textEditor.getText() // Return "Hello World"
    • Step 9: textEditor.redo() // Redo the delete operation. Text = "Hello"
    • Step 10: textEditor.getText() // Return "Hello"
Example 2
  • Input: ["TextEditor", "append", "select", "copy", "append", "select", "paste", "getText", "undo", "getText"] [[], ["Programming"], [0, 3], [], [" is fun"], [14, 16], [], [], [], []]
  • Output: [null, null, null, null, null, null, null, "Programming is Prog", null, "Programming is fun"]
  • Explanation:
    • Step 1: TextEditor textEditor = new TextEditor() // Initialize with empty text
    • Step 2: textEditor.append("Programming") // Text = "Programming"
    • Step 3: textEditor.select(0, 3) // Select "Prog"
    • Step 4: textEditor.copy() // Clipboard = "Prog"
    • Step 5: textEditor.append(" is fun") // Text = "Programming is fun"
    • Step 6: textEditor.select(14, 16) // Select "fun"
    • Step 7: textEditor.paste() // Replace "fun" with "Prog". Text = "Programming is Prog"
    • Step 8: textEditor.getText() // Return "Programming is Prog"
    • Step 9: textEditor.undo() // Undo the paste operation. Text = "Programming is fun"
    • Step 10: textEditor.getText() // Return "Programming is fun"
Constraints
  • 1 ≤ text.length ≤ 10^4
  • 0 ≤ k ≤ 10^4
  • 0 ≤ startend < current text length
  • At most 10^4 calls will be made to the functions.
  • The sum of the lengths of all appended strings will not exceed 10^5.
  • Time Complexity: All operations should be O(n) or better.
  • Space Complexity: O(n) for storing the text and operation history.
StackData StructureTech MahindraLTIMindtree
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use a data structure to store the text (StringBuilder or equivalent).
  • Implement a stack-based system to keep track of operations for undo/redo functionality.
  • Create an Operation class to represent different types of operations and their parameters.
  • Keep track of the current selection for copy/paste operations.
  • Design a state-based system to track changes and allow for undo/redo.

Steps to solve by this approach:

 Step 1: Create a class to represent different types of operations (append, delete, paste) with their parameters

 Step 2: Implement stacks to track operations for undo and redo functionality
 Step 3: For append and delete operations, store information about what was changed and how
 Step 4: For select and copy operations, track the selection range and clipboard content
 Step 5: For paste operations, handle replacing selected text or inserting at the end if no selection

Submitted Code :