Design a Text Editor LeetCode solution in Java

1
class TextEditor {
2
StringBuilder res;
3
int pos = 0;
4

5
public TextEditor() {
6
res = new StringBuilder();
7
}
8

9
public void addText(String text) {
10
res.insert(pos, text);
11
pos += text.length();
12
}
13

14
public int deleteText(int k) {
15
int tmp = pos;
16
pos -= k;
17
if (pos < 0) pos = 0;
18
res.delete(pos, tmp);
19
return tmp - pos;
20
}
21

22
public String cursorLeft(int k) {
23
pos -= k;
24
if (pos < 0) pos = 0;
25
if (pos < 10) return res.substring(0, pos);
26
return res.substring(pos - 10, pos);
27
}
28

29
public String cursorRight(int k) {
30
pos += k;
31
if (pos > res.length()) pos = res.length();
32
if (pos < 10) return res.substring(0, pos);
33
return res.substring(pos - 10, pos);
34
}
35
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0