Design a text editor with a cursor that can perform the following operations:

  • Add text to where the cursor is positioned
  • Delete text from where the cursor is (simulating the backspace key)
  • Move the cursor either left or right

When deleting text, only characters to the left of the cursor will be deleted. The cursor will always remain within the actual text boundaries: 0 ≤ cursor.position ≤ currentText.length.

Implement the TextEditor class with these methods:

  • TextEditor() - Initializes the object with empty text
  • void addText(string text) - Appends text at cursor position, cursor moves to right of added text
  • int deleteText(int k) - Deletes k characters to the left of cursor, returns actual number of characters deleted
  • string cursorLeft(int k) - Moves cursor left k times, returns last min(10, len) characters to the left of cursor
  • string cursorRight(int k) - Moves cursor right k times, returns last min(10, len) characters to the left of cursor

Input & Output

Example 1 — Basic Operations
$ Input: operations = [["TextEditor"],["addText","leetcode"],["deleteText",4],["addText","practice"],["cursorRight",3],["cursorLeft",8]]
Output: [null,null,4,null,"etpractice","leet"]
💡 Note: TextEditor textEditor = new TextEditor(); // Initially text="", cursor=0 textEditor.addText("leetcode"); // text="leetcode", cursor=8 textEditor.deleteText(4); // return 4, text="leet", cursor=4 textEditor.addText("practice"); // text="leetpractice", cursor=12 textEditor.cursorRight(3); // return "etpractice" (cursor at end, last 10 chars) textEditor.cursorLeft(8); // return "leet" (cursor at pos 4, 4 chars to left)
Example 2 — Cursor Movements
$ Input: operations = [["TextEditor"],["addText","hello"],["cursorLeft",3],["addText","world"],["cursorRight",2]]
Output: [null,null,"he",null,"heworldl"]
💡 Note: Start empty, add "hello" (cursor at end), move left 3 positions (cursor after 'e'), add "world", move right 2 positions. Final text: "heworldllo"
Example 3 — Edge Case Boundaries
$ Input: operations = [["TextEditor"],["cursorLeft",5],["addText","a"],["deleteText",10],["cursorRight",5]]
Output: [null,"",null,1,""]
💡 Note: Try to move left from empty (returns ""), add 'a', try to delete 10 chars (only deletes 1), try to move right from empty (returns "")

Constraints

  • 1 ≤ text.length, k ≤ 40
  • text consists of lowercase English letters
  • At most 2 × 104 calls in total will be made to addText, deleteText, cursorLeft and cursorRight

Visualization

Tap to expand
Design a Text Editor INPUT Operations Sequence: 1. TextEditor() 2. addText("leetcode") 3. deleteText(4) 4. addText("practice") 5. cursorRight(3) 6. cursorLeft(8) Initial State: (empty, cursor at 0) Two Stacks: left | right left[] right[] ALGORITHM STEPS 1 Initialize left=[], right=[] 2 addText("leetcode") Push chars to left stack left=[l,e,e,t,c,o,d,e]| 3 deleteText(4) Pop 4 from left, return 4 left=[l,e,e,t]| removed:code 4 addText("practice") Push to left stack left=[l,e,e,t,p,r,a,c,t,i,c,e]| 5 cursorRight(3) --> no change right empty, return last 10 6 cursorLeft(8) Move 8 chars to right stack left=[l,e,e,t]|right=[practice] FINAL RESULT Output Array: [null, null, 4, null, "etpractice", "leet"] Step Results: 1. TextEditor() --> null 2. addText --> null 3. deleteText(4) --> 4 4. addText --> null 5. cursorRight --> "etpractice" 6. cursorLeft --> "leet" Final Text State: l e e t p r a c t i c e OK - All operations O(k) Key Insight: Use TWO STACKS to simulate the cursor: left stack holds chars before cursor, right stack holds chars after. addText: push to left. deleteText: pop from left. cursorLeft: move from left to right. cursorRight: move right to left. This gives O(k) for each operation where k is the number of characters moved/added/deleted. TutorialsPoint - Design a Text Editor | Optimal Two-Stack Approach
Asked in
Google 25 Microsoft 20 Amazon 18 Meta 15
31.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen