177. Maximum Nesting Depth of Parentheses

EasyStringString

Given a valid parentheses string s (which may include letters, digits, and other characters), return the maximum depth of nested parentheses.

Depth increases by 1 each time a '(' is encountered and decreases by 1 at each ')'.

Input: A string s (valid parentheses, may contain other characters).

Output: An integer — the maximum nesting depth.

Examples

Example 1
Input: (1+(2*3)+((8)/4))+1
Output: 3
Explanation: The deepest nesting is (( )) inside — depth 3.
Example 2
Input: (1)+((2))+(((3)))
Output: 3
Explanation: (((3))) has depth 3.
Example 3
Input: 1+(2*3)/(2-1)
Output: 1
Explanation: Single level of parentheses.

Constraints

Asked by

BloombergMetaAmazonGoogleMicrosoft
Solve this problem in the editor →