[LeetCode] Longest Valid Parentheses

简介: This problem is a nice extension of the Valid Parentheses problem. There are several ways to solve it.

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.

 1 class Solution {
 2 public:
 3     int longestValidParentheses (string s) {
 4         stack<int> indexes;
 5         for (int i = 0; i < (int)s.length(); i++) {
 6             if (s[i] == '(' || indexes.empty() || s[indexes.top()] != '(')
 7                 indexes.push(i);
 8             else indexes.pop();
 9         }
10         if (indexes.empty()) return s.length();
11         int maxlen = 0, left = 0, right = s.length() - 1;
12         while (!indexes.empty()) {
13             left = indexes.top();
14             indexes.pop();
15             maxlen = max(maxlen, right - left);
16             right = left - 1;
17         }
18         maxlen = max(maxlen, left);
19         return maxlen;
20     }
21 };

 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:

  1. P[i] = 0 if s[i] == '(' (No valid parentheses end with '(');
  2. P[i] = P[i - 2] + 2 if s[i] == ')' and s[i - 1] == '(', for example, s = '()()';
  3. 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.

 1 class Solution {
 2 public:
 3     int longestValidParentheses(string s) {
 4         vector<int> dp(s.length(), 0);
 5         int maxlen = 0;
 6         for (int i = 1; i < (int)s.length(); i++) {
 7             if (s[i] == ')') {
 8                 if (s[i - 1] == '(')
 9                     dp[i] = 2 + (i - 2 >= 0 ? dp[i - 2] : 0);
10                 else if (i - dp[i - 1] - 1 >= 0 && s[i - dp[i - 1] - 1] == '(')
11                     dp[i] = dp[i - 1] + 2 + (i - dp[i - 1] - 2 >= 0 ? dp[i - dp[i - 1] - 2] : 0);
12                 maxlen = max(maxlen, dp[i]);
13             }
14         }
15         return maxlen;
16     }
17 };

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

 1 class Solution {
 2 public:
 3     int longestValidParentheses(string s) {
 4         vector<int> dp(s.length(), 0);
 5         int maxlen = 0;
 6         for (int i = 1; i < (int)s.length(); i++) {
 7             if (s[i] == ')' && i - dp[i - 1] - 1 >= 0 && s[i - dp[i - 1] - 1] == '(') {
 8                 dp[i] = dp[i - 1] + 2 + (i - dp[i - 1] - 2 >= 0 ? dp[i - dp[i - 1] - 2] : 0);
 9                 maxlen = max(maxlen, dp[i]);
10             }
11         }
12         return maxlen;
13     }
14 };

 

目录
相关文章
|
6月前
Leetcode 516. Longest Palindromic Subsequence
找到一个字符串的最长回文子序列,这里注意回文子串和回文序列的区别。子序列不要求连续,子串(substring)是要求连续的。leetcode 5. Longest Palindromic Substring就是求连续子串的。
25 0
|
6月前
|
Java
Leetcode 3. Longest Substring Without Repeating Characters
此题题意是找出一个不包含相同字母的最长子串,题目给出了两个例子来说明题意,但这两个例子具有误导性,让你误以为字符串中只有小写字母。还好我是心机boy,我把大写字母的情况也给考虑进去了,不过。。。。字符串里竟然有特殊字符,于是贡献了一次wrong answer,这次我把ascii字符表都考虑进去,然后就没问题了。这个故事告诫我们在编程处理问题的时候一定要注意输入数据的范围,面试中可以和面试官去确认数据范围,也能显得你比较严谨。
29 3
LeetCode 424. Longest Repeating Character Replacem
给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次。在执行上述操作后,找到包含重复字母的最长子串的长度。
76 0
LeetCode 424. Longest Repeating Character Replacem
LeetCode 409. Longest Palindrome
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。 在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。
54 0
LeetCode 409. Longest Palindrome
LeetCode 395. Longest Substring with At Least K
找到给定字符串(由小写字符组成)中的最长子串 T , 要求 T 中的每一字符出现次数都不少于 k 。输出 T 的长度。
58 0
LeetCode 395. Longest Substring with At Least K
|
Windows
LeetCode 388. Longest Absolute File Path
我们致力于寻找我们文件系统中文件的最长 (按字符的数量统计) 绝对路径。例如,在上述的第二个例子中,最长路径为 "dir/subdir2/subsubdir2/file2.ext",其长度为 32 (不包含双引号)。
52 0
LeetCode 388. Longest Absolute File Path
LeetCode 367. Valid Perfect Square
给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。
63 0
LeetCode 367. Valid Perfect Square
|
存储
LeetCode 329. Longest Increasing Path in a Matrix
给定一个整数矩阵,找出最长递增路径的长度。 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。
45 0
LeetCode 329. Longest Increasing Path in a Matrix
LeetCode 301. Remove Invalid Parentheses
删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果。 说明: 输入可能包含了除 ( 和 ) 以外的字符。
49 0
LeetCode 301. Remove Invalid Parentheses
|
算法
LeetCode 300. Longest Increasing Subsequence
给定一个无序的整数数组,找到其中最长上升子序列的长度。
34 0
LeetCode 300. Longest Increasing Subsequence