This is a straightforward question and very similar to the 20. Valid Parentheses problem. Except it’s way easier because you don’t have to worry about different parentheses, you only have the (
parentheses.
We use a stack to keep track of starting and ending parentheses and remember the longest length of the stack: that’s our depth.
Note: we are told that the input string is a valid parentheses string. So we don’t have to worry about the ordering of the parentheses.
Code:
Python
Big O Analysis
-
Runtime
The runtime complexity here is
O(N)
as since we would be iterating the array atleast once. -
Memory
The memory usage is
O(N)
since we use a stack to keep track of characters.
— A