-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParenthesisMatch.java
72 lines (54 loc) · 1.61 KB
/
ParenthesisMatch.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
63
64
65
66
67
68
69
70
71
72
/*
Test: [A+25*{Y*(B+C-X)-K}/D*(E+13)+M]
1 [
[
2 [ {
[A + 25 * {
3 [ { (
[A + 25 * { Y * (
4 [ {
[A + 25 * { Y * ( B + C - X )
5 [
[A + 25 * { Y * ( B + C - X ) - K }
6 [ (
[A + 25 * { Y * ( B + C - X ) - K }/ D *
7 [
[A + 25 * { Y * ( B + C - X ) - K }/ D * ( E + 13 )
8
[A + 25 * { Y * ( B + C - X ) - K }/ D * ( E + 13 )+ M ]
*/
//Solution;
import java.util.Stack;
public class ParenthesisMatch{
public static boolean isParenthesisMatch(String str){
if(str.isEmpty()){
return true;
}
Stack<String> stack = new Stack<String>();
String[] strArray = str.split("");
for(int i=0;i<strArray.length;i++){
String first = strArray[i];
if(first.equals("{") || first.equals("(") || first.equals("[")){
stack.push(first);
}
if(first.equals("}") || first.equals(")") || first.equals("]")){
if(stack.isEmpty()){
return false;
}
String last = stack.peek();
if(first.equals("}") && last.equals("{") ||
first.equals(")") && last.equals("(") ||
first.equals("]") && last.equals("[")){
stack.pop();
}else{
return false;
}
}
}
return stack.isEmpty();
}
public static void main(String[] args){
System.out.println(isParenthesisMatch("({})"));
System.out.println(isParenthesisMatch("({(}))"));
}
}