Maximum Nesting Depth of the Parentheses

Solution Code
class Solution:
    def maxDepth(self, s: str) -> int:
        ans, openBrackets = 0, 0
        for c in s:
            if c == '(':
                openBrackets += 1
            elif c == ')':
                openBrackets -= 1
            ans = max(ans, openBrackets)
        return ans

Time Complexity

O(N)

Space Complexity

O(1)