-
Notifications
You must be signed in to change notification settings - Fork 6
/
MatchingParenthesis.java
49 lines (44 loc) · 1009 Bytes
/
MatchingParenthesis.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
import java.util.Stack;
public class MatchingParenthesis {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(new MatchingParenthesis().isValid("(("));
}
public boolean isValid(String s) {
boolean output = true;
char[] charArray = s.toCharArray();
Stack<Character> queue = new Stack<Character>();
char pop = 'c';
if(charArray.length <= 1)
return false;
for (char c : charArray) {
if(c == '(' || c == '{' || c == '['){
queue.push(c);
}else{
if(queue.size()>0){
pop = queue.pop();
}else{
System.out.println("EXTRA");
output = false;
break;
}
if(c == ')' && pop == '('){
continue;
}
else if(c == '}' && pop == '{'){
continue;
}
else if(c == ']' && pop == '['){
continue;
}else{
//System.out.println("POPPP "+pop+" CC "+c+" count "+count);
output = false;
break;
}
}
}
if(queue.size() >0)
output = false;
return output;
}
}