32. Longest Valid Parentheses
1. Description:
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.
2. Difficulty:
Hard(not that hard)
3 .Some Tricky Test Case:
1 | ()()))))()()( |
4. Relative Problems:
1 | 20. Valid Parentheses (easy) |
5. Solution:
Input: String s, return int longest.
(1). use stack:
The main Idea is that we should put all the elements into stack, check whether there is a Valid Parenthese.
if we get s.charAt(i) == ‘(‘ , we should put the index into stack.
if we get s.charAt(i) == ‘)’, we should check top of stack,
if the top of the stack is (, pop it out. Everthing goes well, we get a Valid Parentheses.
But if the top of the stack is ), we should push the index into stack.
1 | e.g. : )()()). |
As we can see, we get all invalid elements index.
If the stack is empty, just need to return s.length.
If the stack is not empty, we need to get longest Valid Parenthese.
6. Code:
1 | public static int longestValidParentheses(String s) { |
(2). Dynamic Programming:
(()()))如果是(,len[i] = 0;如果是),如果上一个是(,则 len[i]=2 (1)如果上一个是),则要看i - len[i] - 1位的情况 (()())即此时最右边,你得看第5 - 4 - 1 = 0位的情况, (2)如果是(,那么len[i] = len[i - 1] + 2;但如果是这种情况()(()),同时我们得看上上一位,即我们新加一个),i - len[i - 1] -2处也可以算入最长序列了。()()或者是这样,说明在(1),(2)两种情况时,都可以获得增长,因此我们最后都要加上len[i - len[i - 1] -2]。
main idea: use array len to store length
if s[i] = ‘(‘, len = 0;
if s[i] = ‘)’, len = the length of near Valid Parentheses.
Analysis:
if, left is ‘(‘, len at least = 2 , we still need to check s[i - 2]. just add len[i - 2].
if, left is ‘)’, we need to check i - len[i - 1] -1, jump to last invalid place to check whether we could get more. if s[i - len[i - 1] - 1] = (; we could get more!
now len[i] = len[i - 1] + 2 + len[i - len[i - 1] - 2]
check len[i - len[i - 1] - 2] to see whether we can get more!
1 | public static int longestValidParentheses_DP(String s){ |
那么我们可以观察得到条件可以合并,i-1 == (不再重要,反正大家都要加2,而且i-1 == (,len【i-1】==0加不加无所谓
Try to observe the if condition, we can get a more concise version!
1 | if(s.charAt(i) == ')'&&i - len[i - 1] - 1 >= 0&& s.charAt(i - len[i - 1] - 1) == '(') |