← DiffPush

Max nesting depth of parenthesis

Standard Bar Strings · Medium O(n) · O(1)

Depth Gauge for the Kernel Trace Line

A kernel debug logger writes each trace as a single balanced line where every nested subroutine call adds one more layer of parentheses around its own sub-expression. The profiler's summary reports how deep the call stack ever gets — the largest number of parentheses that are simultaneously open at any point of the line. Digits and operators are noise; only the brackets move the gauge.

Input: A valid parentheses string s containing digits, the operators '+', '-', '*', '/', and the parentheses '(' and ')'.

Output: The nesting depth of s — the maximum count of simultaneously open parentheses at any point in the string.

Constraints

Examples

Example 1

Input: {"s":"(1+(2*3)+((8)/4))+1"}
Output: 3
The term (8)/4 sits inside three simultaneously open pairs — the deepest point of the line.

Example 2

Input: {"s":"(1)+((2))+(((3)))"}
Output: 3
The three bracket groups nest one, two, and three levels deep, so the deepest group sets the answer.

Solve this in your browser →

Also on LeetCode ↗