会不会有点啰嗦。。。

import java.util.*;

public class Parenthesis {
    public boolean chkParenthesis(String A, int n) {
        if (A.length() != n) return false;
        if (A.length() == 0) return true;
        
        char[] chars = A.toCharArray();
        Stack<Character> stack = new Stack<>();
        for (char aChar : chars) {
            if (aChar == '('){
                stack.push(aChar);
            } else if (aChar == ')'){
                if (!stack.empty() && stack.peek() == '('){
                    stack.pop();
                } else {
                    stack.push(aChar);
                }
            } else{
                return false;
            }
        }
        return stack.empty();
    }
}