-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
54 lines (47 loc) · 1.72 KB
/
Main.java
File metadata and controls
54 lines (47 loc) · 1.72 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int testCase = sc.nextInt();
for(int i=0; i<testCase; i++){
String input = sc.next();
List<Character> left = new ArrayList<Character>();
Stack<Character> right = new Stack<Character>();
for(int j=0; j<input.length(); j++){
if(input.charAt(j) == '<') {
// move an element from left ArrayList to right Stack
if(!left.isEmpty()){
int idx = left.size()-1;
right.push(left.get(idx));
left.remove(idx);
}
} else if(input.charAt(j) == '>'){
// move an element from right Stack to left ArrayList
if(!right.isEmpty()){
left.add(right.pop());
}
} else if(input.charAt(j) == '-') {
// remove an element in left ArrayList
if(!left.isEmpty()){
left.remove(left.size()-1);
}
} else {
// add an element to left ArrayList
left.add(input.charAt(j));
}
}
StringBuilder sb = new StringBuilder();
for(int k=0; k<left.size(); k++){
sb.append(left.get(k));
}
while(!right.isEmpty()){
sb.append(right.pop());
}
System.out.println(sb);
}
sc.close();
}
}