leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

max_nesting_depth_parentheses.py (581B)


# 1614. Maximum Nesting Depth of the Parentheses


class Solution(object):
    def maxDepth(self, s):
        """
        :type s: str
        :rtype: int
        """
        stack = []
        ans = 0
        for c in s:
            if c == "(":
                stack.append(c)
            elif c == ")":
                ans = max(ans, len(stack))
                stack.pop()
            else:
                continue
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(obj.maxDepth("(1+(2*3)+((8)/4))+1"))
    print(obj.maxDepth("(1)+((2))+(((3)))"))