stack November 10, 2022

Maximum nesting depth of the parentheses

Time O(n) Space O(n) Open original problem

We will use a stack to keep track of the maximum depth of the parentheses. We will then iterate through the string and if the current character is an opening parenthesis, we will push the current depth onto the stack. Otherwise, we will pop the top of the stack. We will then return the maximum depth of the stack.

class Solution:
    def maxDepth(self, s: str) -> int:
        res, stack = 0, []
        for c in s:
            if c =='(':
                stack.append(c)
                res = max(res, len(stack))
            elif c == ')':
                stack.pop()
        return res

Time Complexity: O(n)
Space Complexity: O(n)