{ "data": { "question": { "questionId": "2389", "questionFrontendId": "2296", "boundTopicId": null, "title": "Design a Text Editor", "titleSlug": "design-a-text-editor", "content": "
Design a text editor with a cursor that can do the following:
\n\nWhen deleting text, only characters to the left of the cursor will be deleted. The cursor will also remain within the actual text and cannot be moved beyond it. More formally, we have that 0 <= cursor.position <= currentText.length
always holds.
Implement the TextEditor
class:
TextEditor()
Initializes the object with empty text.void addText(string text)
Appends text
to where the cursor is. The cursor ends to the right of text
.int deleteText(int k)
Deletes k
characters to the left of the cursor. Returns the number of characters actually deleted.string cursorLeft(int k)
Moves the cursor to the left k
times. Returns the last min(10, len)
characters to the left of the cursor, where len
is the number of characters to the left of the cursor.string cursorRight(int k)
Moves the cursor to the right k
times. Returns the last min(10, len)
characters to the left of the cursor, where len
is the number of characters to the left of the cursor.\n
Example 1:
\n\n\nInput\n["TextEditor", "addText", "deleteText", "addText", "cursorRight", "cursorLeft", "deleteText", "cursorLeft", "cursorRight"]\n[[], ["leetcode"], [4], ["practice"], [3], [8], [10], [2], [6]]\nOutput\n[null, null, 4, null, "etpractice", "leet", 4, "", "practi"]\n\nExplanation\nTextEditor textEditor = new TextEditor(); // The current text is "|". (The '|' character represents the cursor)\ntextEditor.addText("leetcode"); // The current text is "leetcode|".\ntextEditor.deleteText(4); // return 4\n // The current text is "leet|". \n // 4 characters were deleted.\ntextEditor.addText("practice"); // The current text is "leetpractice|". \ntextEditor.cursorRight(3); // return "etpractice"\n // The current text is "leetpractice|". \n // The cursor cannot be moved beyond the actual text and thus did not move.\n // "etpractice" is the last 10 characters to the left of the cursor.\ntextEditor.cursorLeft(8); // return "leet"\n // The current text is "leet|practice".\n // "leet" is the last min(10, 4) = 4 characters to the left of the cursor.\ntextEditor.deleteText(10); // return 4\n // The current text is "|practice".\n // Only 4 characters were deleted.\ntextEditor.cursorLeft(2); // return ""\n // The current text is "|practice".\n // The cursor cannot be moved beyond the actual text and thus did not move. \n // "" is the last min(10, 0) = 0 characters to the left of the cursor.\ntextEditor.cursorRight(6); // return "practi"\n // The current text is "practi|ce".\n // "practi" is the last min(10, 6) = 6 characters to the left of the cursor.\n\n\n
\n
Constraints:
\n\n1 <= text.length, k <= 40
text
consists of lowercase English letters.2 * 104
calls in total will be made to addText
, deleteText
, cursorLeft
and cursorRight
.\n
Follow-up: Could you find a solution with time complexity of O(k)
per call?
Compiled with clang 11
using the latest C++ 20 standard.
Your code is compiled with level two optimization (-O2
). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.
Most standard library headers are already included automatically for your convenience.
\"], \"java\": [\"Java\", \"OpenJDK 17
. Java 8 features such as lambda expressions and stream API can be used.
Most standard library headers are already included automatically for your convenience.
\\r\\nIncludes Pair
class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.
Python 2.7.12
.
Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.
\\r\\n\\r\\nFor Map/TreeMap data structure, you may use sortedcontainers library.
\\r\\n\\r\\nNote that Python 2.7 will not be maintained past 2020. For the latest Python, please choose Python3 instead.
\"], \"c\": [\"C\", \"Compiled with gcc 8.2
using the gnu11 standard.
Your code is compiled with level one optimization (-O1
). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.
Most standard library headers are already included automatically for your convenience.
\\r\\n\\r\\nFor hash table operations, you may use uthash. \\\"uthash.h\\\" is included by default. Below are some examples:
\\r\\n\\r\\n1. Adding an item to a hash.\\r\\n
\\r\\nstruct hash_entry {\\r\\n int id; /* we'll use this field as the key */\\r\\n char name[10];\\r\\n UT_hash_handle hh; /* makes this structure hashable */\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n\\r\\n\\r\\n\\r\\n
2. Looking up an item in a hash:\\r\\n
\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n struct hash_entry *s;\\r\\n HASH_FIND_INT(users, &user_id, s);\\r\\n return s;\\r\\n}\\r\\n\\r\\n\\r\\n\\r\\n
3. Deleting an item in a hash:\\r\\n
\\r\\nvoid delete_user(struct hash_entry *user) {\\r\\n HASH_DEL(users, user); \\r\\n}\\r\\n\\r\\n\"], \"csharp\": [\"C#\", \"\"], \"javascript\": [\"JavaScript\", \"
Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES6 features.
lodash.js library is included by default.
\\r\\n\\r\\nFor Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.
\"], \"ruby\": [\"Ruby\", \"Ruby 3.1
Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms
\"], \"swift\": [\"Swift\", \"Swift 5.5.2
.
Go 1.21
Support https://godoc.org/github.com/emirpasic/gods@v1.18.1 library.
\"], \"python3\": [\"Python3\", \"Python 3.10
.
Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.
\\r\\n\\r\\nFor Map/TreeMap data structure, you may use sortedcontainers library.
\"], \"scala\": [\"Scala\", \"Scala 2.13.7
.
Kotlin 1.9.0
.
Rust 1.58.1
Supports rand v0.6\\u00a0from crates.io
\"], \"php\": [\"PHP\", \"PHP 8.1
.
With bcmath module
\"], \"typescript\": [\"Typescript\", \"TypeScript 5.1.6, Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES2022 features.
lodash.js library is included by default.
\"], \"racket\": [\"Racket\", \"Run with Racket 8.3
.
Dart 2.17.3
\\r\\n\\r\\nYour code will be run directly without compiling
\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }