Interactive visualization of finding the longest valid parentheses substring
-1 as base. For each ), pop the top. If stack is empty, push current index as new base. Otherwise, maxLen = i - stack.top. Time: O(n), Space: O(n)
dp[i] = length of longest valid substring ending at index i.
Case 1: s[i-1]=='(' then dp[i] = dp[i-2] + 2.
Case 2: s[i-1]==')' and s[i-dp[i-1]-1]=='(' then dp[i] = dp[i-1] + 2 + dp[i-dp[i-1]-2].
Time: O(n), Space: O(n)