-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelimiterSoup.java
More file actions
64 lines (60 loc) · 2.11 KB
/
Copy pathDelimiterSoup.java
File metadata and controls
64 lines (60 loc) · 2.11 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
import java.util.Scanner;
import java.util.Stack;
public class DelimiterSoup {
public static void main(String[] args) {
// if opening, push to stack
// if closing, pop, check if they are equal
Scanner sc = new Scanner(System.in);
Stack<Character> stack = new Stack<>();
int n = Integer.parseInt(sc.nextLine());
// put everything in a char array an iterate over it
char[] line = sc.nextLine().toCharArray();
char temp;
for (int i = 0; i < n; i++) {
switch (line[i]){
case '(':
case '[':
case '{':
stack.push(line[i]);
break;
case ')':
if(stack.size() == 0){
System.out.println(line[i] + " " + (i));
return;
}
temp = stack.pop();
if (temp != '('){
System.out.println(line[i] + " " + (i));
return;
}
break;
case ']':
if(stack.size() == 0){
System.out.println(line[i] + " " + (i));
return;
}
temp = stack.pop();
if (temp != '['){
System.out.println(line[i] + " " + (i));
return;
}
break;
case '}':
if(stack.size() == 0){
System.out.println(line[i] + " " + (i));
return;
}
temp = stack.pop();
if (temp != '{'){
System.out.println(line[i] + " " + (i));
return;
}
break;
}
}
// if(stack.size() > 0){
// System.out.println(stack.pop() + " " + ());
// }
System.out.println("ok so far");
}
}