-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBraceChecker.java
62 lines (55 loc) · 1.59 KB
/
BraceChecker.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
import java.util.Deque;
import java.util.LinkedList;
/**
* <pre>
* I was thinking about the shunting-yard algorithm when I was doing this.
* Because I had been exposed to more complex algorithms, I wasn't even
* thinking that you can do this with just a simple regex because there
* are nothing but braces (nothing inside)! Dumb.
*
* This shows how intelligent people (not saying I am or am not one) can
* overlook simple solutions.
* </pre>
*
* @author Jonathan Bradley Whited
* @see https://www.codewars.com/kata/valid-braces/java
* @see https://en.wikipedia.org/wiki/Shunting-yard_algorithm
* @rank 6 kyu
*/
public class BraceChecker {
public static void main(String[] args) {
BraceChecker checker = new BraceChecker();
String[] testers = new String[]{
"()", // true
"[(])" // false
};
for(String tester: testers) {
System.out.println(tester + "? " + checker.isValid(tester));
}
for(String arg: args) {
System.out.println(arg + "? " + checker.isValid(arg));
}
}
public boolean isValid(String braces) {
Deque<Character> open = new LinkedList<>();
for(int i = 0; i < braces.length(); ++i) {
char c = braces.charAt(i);
char openBrace = getOpenBrace(c);
if(openBrace == 0) {
open.push(c);
}
else if(open.isEmpty() || open.pop() != openBrace) {
return false;
}
}
return open.isEmpty();
}
public char getOpenBrace(char closeBrace) {
switch(closeBrace) {
case ')': return '(';
case ']': return '[';
case '}': return '{';
}
return 0;
}
}