|
@@ -1,4 +1,71 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
3
|
3
|
public class ParenChecker {
|
4
|
|
-}
|
|
4
|
+
|
|
5
|
+ public static class stack{
|
|
6
|
+ int top =-1;
|
|
7
|
+ char items[] = new char[100];
|
|
8
|
+
|
|
9
|
+ public void push(char x){
|
|
10
|
+ if(top == 99){
|
|
11
|
+ System.out.println("Stack full");
|
|
12
|
+ }
|
|
13
|
+ else{
|
|
14
|
+ items[++top] = x;
|
|
15
|
+ }
|
|
16
|
+ }
|
|
17
|
+ char pop(){
|
|
18
|
+ if(top == -1){
|
|
19
|
+ System.out.println("Underflow error");
|
|
20
|
+ return '\0';
|
|
21
|
+ }
|
|
22
|
+ else{
|
|
23
|
+ char element = items[top];
|
|
24
|
+ top--;
|
|
25
|
+ return element;
|
|
26
|
+ }
|
|
27
|
+ }
|
|
28
|
+
|
|
29
|
+ public boolean isEmpty(){
|
|
30
|
+ return (top ==-1);
|
|
31
|
+ }
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ public static boolean isMatchingPar(char character1, char character2){
|
|
35
|
+ if(character1 == '(' && character2 == ')')
|
|
36
|
+ return true;
|
|
37
|
+ else if(character1 == '{' && character2 == '}')
|
|
38
|
+ return true;
|
|
39
|
+ else if(character1 == '[' && character2 == ']')
|
|
40
|
+ return true;
|
|
41
|
+ else
|
|
42
|
+ return false;
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ public static boolean areParenthesisBalanced(char exp[]){
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+ stack st = new stack();
|
|
49
|
+
|
|
50
|
+ for(int i = 0; i< exp.length; i++){
|
|
51
|
+ if(exp[i]=='{' || exp[i] == '(' || exp[i] == '[')
|
|
52
|
+ st.push(exp[i]);
|
|
53
|
+ if(exp[i]=='}' || exp[i]==')' || exp[i] ==']'){
|
|
54
|
+ if(st.isEmpty()){
|
|
55
|
+ return false;
|
|
56
|
+ }
|
|
57
|
+ else if(!isMatchingPar(st.pop(), exp[i])){
|
|
58
|
+ return false;
|
|
59
|
+ }
|
|
60
|
+ }
|
|
61
|
+ }
|
|
62
|
+ if(st.isEmpty())
|
|
63
|
+ return true;
|
|
64
|
+ else{
|
|
65
|
+ return false;
|
|
66
|
+ }
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+ }
|
|
71
|
+
|