Implement an expression evaluator for a simplified calculator.
Given a string s, return the integer value of the expression. The expression may contain:
-
Non-negative integers
-
The binary operators
+
and
-
-
Parentheses
(
and
)
-
Arbitrary spaces
Unary signs may appear at the beginning of the expression or immediately after an opening parenthesis, such as -3 + (2 - 5).
Examples:
Input: "1 + 2 - 3"
Output: 0
Input: "(1 + (4 - 2)) - 3"
Output: 0
Input: "-2 + (3 - 1)"
Output: 0
Follow-up: add string validation. Before evaluating, verify that the expression is syntactically valid. Reject expressions with invalid characters, mismatched parentheses, missing operands, consecutive binary operators, or malformed numbers. Define and document how your function reports invalid input, for example by returning an error or throwing an exception.