[Leetcode] Longest Valid parentheses

Source: Internet
Author: User

This problem are a nice extension of the Valid parentheses problem.

There is several ways to solve it. The first idea was 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 and valid parentheses.

Let's see an example "()(()" first. In this example, it's clear that the first and the valid and the last are also valid. Thus the third characters seperate and valid parentheses. We push its index 3 into the stack. Then the longest valid parentheses are either on the left side of it or on the right side of it. For strings that has more than one such indexes, like "())) ()) ()", we push all of the 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 on Valid parentheses. Each time a mismatch occurrs, the current index is and what we need.

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

`1 classSolution {2  Public:3     intLongestvalidparentheses (strings) {4stack<int>indexes;5          for(inti =0; I < (int) s.length (); i++) {6             if(S[i] = ='('|| Indexes.empty () | | S[indexes.top ()]! ='(')7 Indexes.push (i);8             ElseIndexes.pop ();9         }Ten         if(Indexes.empty ())returns.length (); One         intMaxLen =0, left =0, right = S.length ()-1; A          while(!Indexes.empty ()) { -left =indexes.top (); - Indexes.pop (); theMaxLen = max (MaxLen, right-Left ); -right = left-1; -         } -MaxLen =Max (MaxLen, left); +         returnMaxLen; -     } +};`

Of course, this problem also have a Dynamic programming solution. Let's define P[i] to being the length of the longest valid parentheses end at I. Then state equations is:

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] == ‘)‘` `s[i - P[i - 1] - 1] == ‘(‘` and, for example, s = ' (()) '.

Putting these together, we have the following code.

`1 classSolution {2  Public:3     intLongestvalidparentheses (strings) {4vector<int> dp (s.length (),0);5         intMaxLen =0;6          for(inti =1; I < (int) s.length (); i++) {7             if(S[i] = =')') {8                 if(S[i-1] =='(') {9Dp[i] =2+ (I >=2? Dp[i-2] :0);TenMaxLen =Max (MaxLen, Dp[i]); One                 } A                 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]); the                 } -             } -         } -         returnMaxLen; +     } -};`

[Leetcode] Longest Valid parentheses

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• Sales Support

1 on 1 presale consultation

• After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.