-
Notifications
You must be signed in to change notification settings - Fork 4
/
ParenthesisChecker.java
61 lines (54 loc) · 1.6 KB
/
ParenthesisChecker.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
package string;
import java.util.HashMap;
import java.util.Stack;
import utils.FunStringAlgorithm;
/**
* Check whether parentheses are properly matched in a given String.
*
* We can assume the parentheses are '()', '{}', '[]' and '<>' only.
*
* This is useful is code lint tools.
*
* @author ruifengm
* @since 2018-May-11
*
* YiTu tech interview round 2
*/
public class ParenthesisChecker extends FunStringAlgorithm {
/**
* A very typical use case of the stack data structure
*/
public static boolean checkParenMatched(String s) {
// Store all parenthesis matches into a hashmap
HashMap<Character, Character> hash = new HashMap<>();
hash.put(')', '(');
hash.put(']', '[');
hash.put('}', '{');
hash.put('>', '<');
// Check
Stack<Character> stack = new Stack<>();
for (char c: s.toCharArray()) {
Character cObj = new Character(c);
if (hash.values().contains(cObj)) stack.push(cObj);
if (hash.keySet().contains(cObj)) {
if(!stack.isEmpty() && stack.pop().equals(hash.get(cObj)));
else return false;
}
}
if (stack.isEmpty()) return true;
else return false;
}
public static void main(String[] args) {
// String testStr = "";
// String testStr = ")]}";
// String testStr = "()]}";
// String testStr = ")]{}";
// String testStr = "{[";
// String testStr = "()";
String testStr = "{this[is(a<test>)]}";
System.out.println("Welcome to the rabbit hole of parenthesis checkers! \n"
+ "The test string is: \n" + testStr+ "\n");
System.out.println("Parentheses matched? " + checkParenMatched(testStr));
System.out.println("All rabbits gone.");
}
}