Practice Design a Text Editor
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Design a Text Editor
Problem Statement
TextEditor starts empty with its cursor at zero. addText(text) inserts at the cursor and moves it after the inserted text. deleteText(k) removes up to k characters immediately left of the cursor and returns the actual count. cursorLeft(k) and cursorRight(k) move within text bounds and return at most ten characters immediately left of the new cursor. Practice approach: Keep two stacks for text left and right of the cursor. Insert/delete on the left stack and transfer characters between stacks when moving. The last ten left-stack entries form the returned preview. Test deleting past the start and inserting between existing characters.
Source: Design a Text Editor on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
["TextEditor","addText","cursorLeft","addText","cursorRight","deleteText","cursorLeft"] [[],["abcd"],[2],["xy"],[1],[3],[9]]
[null,null,"ab",null,"abxyc",3,""]
The intermediate text is abxyc|d. Deleting three characters produces ab|d, then moving left stops at the beginning.
Constraints
Related LeetCode Problems
Practice more problems similar to Design a Text Editor.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.