
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 ≤ start ≤ end < 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.
Editorial
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. |
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.