forked from sunstick/code-street
-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest_valid_paren.cpp
38 lines (30 loc) · 1007 Bytes
/
longest_valid_paren.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
For "(()", the longest valid parentheses substring is "()", which has length = 2.
Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.
*/
class Solution {
public:
int longestValidParentheses(string s) {
int n = s.size();
vector<int> dp(n + 1);
vector<int> vi;
int maxlen = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '(') {
vi.push_back(i);
dp[i + 1] = 0;
} else {
if (vi.empty())
dp[i + 1] = 0;
else {
int p = vi.back();
vi.pop_back();
dp[i + 1] = i - p + 1 + dp[p];
maxlen = max(maxlen, dp[i + 1]);
}
}
}
return maxlen;
}
};