We can use a stack to store the indices of characters. We push the index of ( and when we see ), we pop. The length is current_index - stack[-1]. We seed the stack with -1 to handle the base starting case.
stack = [-1],max_len = 0.- Iterate
ifrom 0 tolen(s) - 1:- If
s[i] == '(':stack.append(i). - Else (
s[i] == ')'):stack.pop().- If
stackis empty, this)is a breaker. Push currentias the new ground. - Else, current valid length is
i - stack[-1]. Updatemax_len.
- If
- Time Complexity: O(N).
- Space Complexity: O(N).
def longest_valid_parentheses(s):
stack = [-1]
max_len = 0
for i in range(len(s)):
if s[i] == '(':
stack.append(i)
else:
stack.pop()
if not stack:
stack.append(i)
else:
max_len = max(max_len, i - stack[-1])
return max_len