您的位置:首页 > 其它

[LeetCode] Longest Valid Parentheses

2015-06-13 15:29 405 查看
This problem is a nice extension of the Valid Parentheses problem.

There are several ways to solve it. The first idea is also to use a stack. However, in this time, we push the index instead of the character itself into the stack. What indexes do we push? Well, we push the indexes which seperate two valid parentheses.

Let's see an example "()(()" first. In this example, it is clear that the first two are valid and the last two are also valid. Thus the third characters seperate two valid parentheses. We push its index 3 into the stack. Then the longest valid parentheses is either on the left side of it or on the right side of it. For strings that have more than one such indexes, like "())())()", we push all of them into the stack. Then we visit each valid parentheses separated by them and find the longest length.

How do we obtain these indexes? In fact, you can simply follow the way in Valid Parentheses. Each time a mismatch occurrs, the current index is what we need.

You may run the following code on the above examples to get an understanding of how it works.

class Solution {
public:
int longestValidParentheses (string s) {
stack<int> indexes;
for (int i = 0; i < (int)s.length(); i++) {
if (s[i] == '('|| indexes.empty() || s[indexes.top()] != '(')
indexes.push(i);
else indexes.pop();
}
if (indexes.empty()) return s.length();
int maxlen = 0, left = 0, right = s.length() - 1;
while (!indexes.empty()) {
left = indexes.top();
indexes.pop();
maxlen = max(maxlen, right - left);
right = left - 1;
}
maxlen = max(maxlen, left);
return maxlen;
}
};


Of course, this problem also has a Dynamic Programming solution. Let's define P[i] to be the length of the longest valid parentheses end at i. Then state equations are:

P[i] = 0
if
s[i] == '('
(No valid parentheses end with '(');

P[i] = P[i - 2] + 2
if
s[i] == ')'
and
s[i - 1] == '('
, for example, s = '()()';

P[i] = P[i - P[i - 1] - 2] + P[i - 1] + 2
if
s[i] == ')'
and
s[i - 1] == ')'
and
s[i - P[i - 1] - 1] == '('
, for example, s = '(())'.

Putting these together, we have the following code.

class Solution {
public:
int longestValidParentheses(string s) {
vector<int> dp(s.length(), 0);
int maxlen = 0;
for (int i = 1; i < (int)s.length(); i++) {
if (s[i] == ')') {
if (s[i - 1] == '(')
dp[i] = 2 + (i - 2 >= 0 ? dp[i - 2] : 0);
else if (i - dp[i - 1] - 1 >= 0 && s[i - dp[i - 1] - 1] == '(')
dp[i] = dp[i - 1] + 2 + (i - dp[i - 1] - 2 >= 0 ? dp[i - dp[i - 1] - 2] : 0);
maxlen = max(maxlen, dp[i]);
}
}
return maxlen;
}
};


You may even notice that case 3 above has already includede case 2. So the code can be further shorten to below.

class Solution {
public:
int longestValidParentheses(string s) {
vector<int> dp(s.length(), 0);
int maxlen = 0;
for (int i = 1; i < (int)s.length(); i++) {
if (s[i] == ')' && i - dp[i - 1] - 1 >= 0 && s[i - dp[i - 1] - 1] == '(') {
dp[i] = dp[i - 1] + 2 + (i - dp[i - 1] - 2 >= 0 ? dp[i - dp[i - 1] - 2] : 0);
maxlen = max(maxlen, dp[i]);
}
}
return maxlen;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: