#C3927. Maximum Valid Parentheses Depth
Maximum Valid Parentheses Depth
Maximum Valid Parentheses Depth
Given a string that represents a mathematical expression, determine the maximum depth of valid nested parentheses. The expression contains only digits, the operators +, -, *, /, and parentheses ( )
. If the input string contains any invalid characters (i.e. characters other than 0-9, +, -, *, /, (, )) or if the parentheses are not properly balanced, output -1
.
For example, for the expression \( (1+(2*3)+((8)/4))+1 \), the maximum depth of valid nested parentheses is 3.
Note: All formulas should be written in \( \LaTeX \) format when applicable.
inputFormat
The input consists of a single line containing the expression string.
outputFormat
Output a single integer: the maximum depth of valid nested parentheses if the expression is valid, or -1
if it contains any invalid characters or unbalanced parentheses.
(1+(2*3)+((8)/4))+1
3
</p>