Python Nesting
def nesting(s: str) -> int:
if not s:
return 1
stack: list[str] = []
for ch in s:
if ch == ")":
if not stack or stack.pop() != "(":
return 0
elif ch:
stack.append(ch)
return 1 if not stack else 0
This treats the string like a balance counter: open parentheses add one, closing ones remove one.