1 import java.util.Stack;
2
3 public class Parentheses {
4 public static boolean isParentheses(String s) {
5 Stack<Character> stack = new Stack<>();
6 for (int i = 0; i < s.length(); i++) {
7 char c = (char) s.charAt(i);
8 if (c == '{' || c == '[' || c == '(') {
9 stack.push(c);
10 } else if (c == ')') {
11 if (stack.peek() != '(')
12 return false;
13 else
14 stack.pop();
15 } else if (c == ']') {
16 if (stack.peek() != '[')
17 return false;
18 else
19 stack.pop();
20 } else if (c == '}') {
21 if (stack.peek() != '{')
22 return false;
23 else
24 stack.pop();
25 }
26 }
27 return true;
28 }
29
30 public static void main(String[] args) {
31 // TODO Auto-generated method stub
32 System.out.println(isParentheses("[(])"));
33 System.out.println(isParentheses("[()]{}{[()()]()}"));
34 }
35
36 }