-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleTextEditor.java
119 lines (102 loc) · 4.56 KB
/
simpleTextEditor.java
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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
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. */
Stack<String> stack = new Stack<String>();
Scanner s = new Scanner(System.in);
int n = s.nextInt();
String editor = "";
for(int i=1; i <= n; i++) // loop for n operations
{
int operation = s.nextInt(); // input which operation to perform
if(operation == 1)
{
String appendingStr = s.nextLine();
appendingStr = appendingStr.substring(1,appendingStr.length());
editor = editor + appendingStr; // append string to existing string
String memo = operation + "-" + appendingStr; // push current operation in stack with (operation +appendedstring) for furthur reference
stack.push(memo);
//System.out.print(editor);
}
if(operation == 2)
{
int numOfDelete = s.nextInt();
String toDelete = editor.substring(editor.length()-numOfDelete, editor.length()); // find delete string
if(toDelete.equals(editor))
{
editor = "";
}
else{
editor = editor.substring(0, editor.length()-numOfDelete); // update original string after deleting
}
String memo = operation + "-" + toDelete; // push (operation + deleted string) to stack
stack.push(memo);
//System.out.print(editor);
}
if(operation == 3)
{
System.out.println(editor.charAt(s.nextInt()-1)); // print char at specific index
}
if(operation == 4)
{
String reverseOperation = stack.peek(); // peek top of stack
if(reverseOperation.charAt(0) == '1') // if previous operation is append then delete and vise varsa
{
editor = editor.substring(0, editor.length() - (reverseOperation.length() - 2));
}
if(reverseOperation.charAt(0) == '2')
{
editor = editor + reverseOperation.substring(2,reverseOperation.length());
}
stack.pop(); // pop top of the stack
}
}
s.close();
//stack.push(editor);
try{
InputStreamReader inputStream = new InputStreamReader(System.in); // everything is same but used InputStreamReader for faster opearation
BufferedReader s = new BufferedReader(inputStream);
int n = Integer.parseInt(s.readLine());
String editor = "";
stack.push(editor);
for(int i=1; i <= n; i++)
{
//int operation = s.nextInt();
String input = s.readLine();
String[] inputArr = input.split(" ");
int operation = Integer.parseInt(inputArr[0]);
if(operation == 1)
{
String appendingStr = inputArr[1];
//stack.push(appendingStr.substring(1,appendingStr.length()));
editor = editor + appendingStr.substring(1,appendingStr.length());
stack.push(editor);
}
if(operation == 2)
{
int numOfDelete = Integer.parseInt(inputArr[1]);
editor = editor.substring(0, editor.length()-numOfDelete);
stack.push(editor);
}
if(operation == 3)
{
int printIndex = Integer.parseInt(inputArr[1]);
System.out.println(editor.charAt(printIndex - 1));
}
if(operation == 4)
{
stack.pop();
editor = stack.peek();
//System.out.println(editor);
}
}
}catch (NumberFormatException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}