-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleTextEditor.java
More file actions
137 lines (116 loc) · 4.33 KB
/
SimpleTextEditor.java
File metadata and controls
137 lines (116 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
Implement a simple text editor that supports the following operations:
Append: Append a given string to the end of the current text.
Delete: Delete the last k characters from the current text.
Print: Print the character at a given 1-based index.
Undo: Undo the last append or delete operation and revert the text to its previous state.
Input Format:
The first line contains an integer q, the number of operations.
The next q lines describe the operations in one of the following formats:
1 w: Append the string w to the text.
2 k: Delete the last k characters from the text.
3 k: Print the k-th character of the text (1-based index).
4: Undo the last append or delete operation.
Output Format:
For each 3 k operation, output the k-th character of the text.
Constraints:
The sum of the lengths of all w across all append operations does not exceed 106106.
The sum of k across all delete operations does not exceed 106106.
It is guaranteed that the sequence of operations is valid.
*/
// This code is correct, but it doesn't pass all test cases, it passes 14/15 test cases
// Meaning there's only 1 test case that doesn't pass. I will work it through, I'll update when I figured the solution.
// Also when working on this problem; you can use a switch instead of if-else statements.
import java.io.*;
import java.util.*;
public class Solution
{
public static void main(String[] args)
{
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner scan = new Scanner(System.in);
int q = scan.nextInt();
StringBuilder s = new StringBuilder();
Stack<String> addText = new Stack<>();
for(int i = 0; i < q; i++)
{
int typeOfOperation = scan.nextInt();
if(typeOfOperation == 1)
{
addText.push(s.toString());
String w = scan.next();
s.append(w);
}
else if(typeOfOperation == 2)
{
addText.push(s.toString());
int k = scan.nextInt();
s.delete(s.length() - k, s.length());
}
else if(typeOfOperation == 3)
{
int operationIndex = scan.nextInt();
System.out.println(s.charAt(operationIndex - 1));
}
else
{
if(!addText.isEmpty())
{
s = new StringBuilder(addText.pop());
}
}
}
scan.close();
}
}
// Using a switch statement
// The switch passes 13/15 test cases. Meaning two cases don't successfuly pass this code.
import java.io.*;
import java.util.*;
public class Solution
{
public static void main(String[] args)
{
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner scan = new Scanner(System.in);
int q = scan.nextInt();
StringBuilder s = new StringBuilder();
Stack<String> addText = new Stack<>();
for(int i = 0; i < q; i++)
{
int typeOfOperation = scan.nextInt();
switch(typeOfOperation)
{
case 1:
{
addText.push(s.toString());
String w = scan.next();
s.append(w);
break;
}
case 2:
{
addText.push(s.toString());
int k = scan.nextInt();
s.delete(s.length() - k, s.length());
break;
}
case 3:
{
int operationIndex = scan.nextInt();
System.out.println(s.charAt(operationIndex - 1));
break;
}
case 4:
{
if(!addText.isEmpty())
{
s = new StringBuilder(addText.pop());
break;
}
}
}
}
scan.close();
}
}