-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParanthesisCheck.java
65 lines (63 loc) · 1.41 KB
/
ParanthesisCheck.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
package codingProblems;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class ParanthesisCheck {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
System.out.println("Please Enter Your String");
String s=in.nextLine();
System.out.println(checkParan(s));
in.close();
}
public static boolean checkParan(String s){
Stack<Character> st=new Stack<Character>();
boolean ans=false;
Character[] openParan={'(','[','{'};
Character[] closeParan={')',']','}'};
List<Character> openParList = Arrays.asList(openParan);
List<Character> closeParList = Arrays.asList(closeParan);
if(s==null){
return ans;
}
else if(s.length()==0){
ans=true;
return ans;
}
else{
for(int i=0;i<s.length();i++){
char temp=s.charAt(i);
if(openParList.contains(temp)){
st.push(temp);
}
else if(closeParList.contains(temp)){
if(!st.empty()){
if(st.peek()!='(' && temp==')'){
return ans;
}
else if(st.peek()!='[' && temp==']'){
return ans;
}
else if(st.peek()!='{' && temp=='}'){
return ans;
}
else
st.pop();
}
else
return ans;
}
else
return ans;
}
if( st.empty()){
ans = true;
return ans;
}else{
return ans;
}
}
}
}