-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClass.java
More file actions
55 lines (45 loc) · 1.61 KB
/
MainClass.java
File metadata and controls
55 lines (45 loc) · 1.61 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
class Solution {
public boolean isValid(String s) {
char[] stack = new char[s.length()];
int top = -1;
HashMap<Character, Character> symbolMap = new HashMap<Character, Character>();
symbolMap.put('(', ')');
symbolMap.put('{', '}');
symbolMap.put('[', ']');
if (s.length()%2 > 0) return false;
for (char symbol : s.toCharArray()) {
System.out.println("symbol: " + symbol);
switch (symbol) {
case '(':
case '{':
case '[':
top++;
stack[top] = symbolMap.get(symbol);
break;
default:
if (top <0) return false;
if (symbol != stack[top]) return false;
top--;
}
}
return !(top > 0);
}
}
public class MainClass {
public static String stringToString(String input) {
return JsonArray.readFrom("[" + input + "]").get(0).asString();
}
public static String booleanToString(boolean input) {
return input ? "True" : "False";
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
String s = stringToString(line);
boolean ret = new Solution().isValid(s);
String out = booleanToString(ret);
System.out.print(out);
}
}
}