← 返回 meta 的题目列表Maximum Depth of Balanced Parentheses
类型:online_judge
Write a function that accepts a string with sets of parentheses and returns the maximum depth of balanced parentheses. If the parentheses are not balanced, return -1.
Input
A string, e.g., ( p(q) ((s)t) )
Output
An integer representing the maximum depth, or -1 if the parentheses are not balanced.
Examples
Input: ( p(q) ((s)t) )
Output: 3
Input: r) (k) ()
Output: -1
Notes
The string may contain other characters besides parentheses that do not affect the depth calculation.
Example
Input
( p(q) ((s)t) )