-
Notifications
You must be signed in to change notification settings - Fork 14
/
ValidParentheses.java
38 lines (35 loc) · 1019 Bytes
/
ValidParentheses.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
import java.util.*;
public class ValidParentheses {
public static boolean isValid(String str) {
Stack<Character> s = new Stack<>();
int i=0;
while(i<str.length()) {
char ch = str.charAt(i);
if(ch == '(' || ch == '{' || ch == '[') {
s.push(ch);
} else {
if(s.isEmpty()) {
return false;
}
char top = s.peek();
if((top == '(' && ch == ')')
|| (top == '{' && ch == '}')
|| (top == '[' && ch == ']')) {
s.pop();
} else {
return false;
}
}
i++;
}
if(!s.isEmpty()) {
return false;
}
return true;
}
public static void main(String args[]) {
// String str = "(([{}])()[])";
String str = "(([{}])))[])";
System.out.println(isValid(str));
}
}