
Problem
Solution
Submissions
Text Editor
Certification: Advanced Level
Accuracy: 0%
Submissions: 0
Points: 20
Write a C++ program to implement a text editor that supports basic operations like append, delete, and cursor movement along with undo and redo functionality.
Example 1
- Input: ["TextEditor", "append", "cursorRight", "append", "cursorLeft", "delete", "undo", "redo", "getText"] [[], ["Hello"], [2], [" World"], [3], [2], [], [], []]
- Output: [null, null, null, null, null, null, null, null, "Hell World"]
- Explanation:
- TextEditor editor = new TextEditor();
- editor.append("Hello"); // text = "Hello|" (cursor at end of text)
- editor.cursorRight(2); // can only move cursor right 0 positions
- editor.append(" World"); // text = "Hello World|"
- editor.cursorLeft(3); // text = "Hello Wo|rld"
- editor.delete(2); // text = "Hello |rld"
- editor.undo(); // text = "Hello Wo|rld"
- editor.redo(); // text = "Hello |rld"
- editor.getText(); // returns "Hell World"
Example 2
- Input: ["TextEditor", "append", "append", "delete", "cursorLeft", "cursorRight", "undo", "getText"] [[], ["ABC"], ["DEF"], [1], [5], [2], [], []]
- Output: [null, null, null, null, null, null, null, "ABCDEF"]
- Explanation:
- TextEditor editor = new TextEditor();
- editor.append("ABC"); // text = "ABC|"
- editor.append("DEF"); // text = "ABCDEF|"
- editor.delete(1); // text = "ABCDE|"
- editor.cursorLeft(5); // text = "|ABCDE"
- editor.cursorRight(2); // text = "AB|CDE"
- editor.undo(); // text = "ABCDEF|" (undoes the delete operation)
- editor.getText(); // returns "ABCDEF"
Constraints
- 1 <= text.length <= 100
- 1 <= k <= 100
- The editor initially starts with an empty text.
- The undo and redo operations return the state of the editor before and after the corresponding operation.
- At most 2000 calls will be made to all operations combined.
- Time Complexity: O(1) amortized for all operations
- Space Complexity: O(N) where N is the total length of all text operations
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 two stacks for undo and redo operations.
- Use a string or a data structure optimized for insertions and deletions.
- Track the cursor position carefully within bounds.
- Each operation should be recorded for undo/redo functionality.
- Consider using a doubly-linked list or a gap buffer for text representation.