import java.util.*;
public class Parenthesis {
public boolean chkParenthesis(String A, int n) {
// write code here
if(n % 2 != 0) {
return false;
}
Stack<Character> stack = new Stack<>();
for(char ch : A.toCharArray()) {
if(ch == '(') {
stack.push(ch);
} else if(ch == ')') {
if(stack.isEmpty()) {
return false;
} else if(stack.peek() == '(') {
stack.pop();
}
} else {
return false;
}
}
return stack.isEmpty();
}
}

京公网安备 11010502036488号