Back to the Problem
Track the nesting depth; it must never go negative and must end at zero.
Complexity: O(|S|)