-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParanthesisMatching.java
More file actions
33 lines (31 loc) · 1.17 KB
/
ParanthesisMatching.java
File metadata and controls
33 lines (31 loc) · 1.17 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
import java.util.*;
public class ParanthesisMatching{
static Stack<Character> checkStack = new Stack<>();
static Boolean isBracketmatching(char open,char close){
return (((open == '(')&&(close == ')'))||((open == '{')&&(close == '}'))||((open == '[') &&(close == ']')));
}
static Boolean isMatch(String s){
if(s.length()==1 || s.length()==0){
return false;
}
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
if((c == '(')||(c == '{')||(c == '[')){ //Check the open bracket
checkStack.push(c);
}
else if((c == '}')||(c == ')')||(c == ']')){ //Check the close bracket
if(checkStack.isEmpty()){
return false;
}
char top = checkStack.pop();
if(!isBracketmatching(top, c)){ //CheCks the bracket matching
return false;
}
}
}
return checkStack.isEmpty();
}
public static void main(String[] args) {
System.out.println(isMatch("}}}}}}}}}}")?"The String is Balanced ":"The String is not Balanced");
}
}