Design a Text Editor LeetCode solution in Python

1
# Runtime: 1865 ms (Top 50.36%) | Memory: 27.8 MB (Top 82.98%)
2
class TextEditor:
3

4
def __init__(self):
5
self.s = ""
6
self.cursor = 0
7

8
def addText(self, text: str) -> None:
9
self.s = self.s[: self.cursor] + text + self.s[self.cursor :]
10
self.cursor += len(text)
11

12
def deleteText(self, k: int) -> int:
13
new_cursor = max(0, self.cursor - k)
14
noOfChars = k if self.cursor - k >= 0 else self.cursor
15
self.s = self.s[:new_cursor] + self.s[self.cursor :]
16
self.cursor = new_cursor
17
return noOfChars
18

19
def cursorLeft(self, k: int) -> str:
20
self.cursor = max(0, self.cursor - k)
21
start = max(0, self.cursor - 10)
22
return self.s[start : self.cursor]
23

24
def cursorRight(self, k: int) -> str:
25
self.cursor = min(len(self.s), self.cursor + k)
26
start = max(0, self.cursor - 10)
27
return self.s[start : self.cursor]

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0