public class ValidParentheses {
public static int isValid(String s) {
Stack<Character> stack = new Stack<>();
for (char c : s.toCharArray()) {
if (c == '(') {
stack.push(')');
}
else if (stack.isEmpty() || stack.pop() != c) {
return -1;
}
}
return s.length() / 2;
}
}