Longest Valid parentheses

Source: Internet
Author: User

https://leetcode.com/problems/longest-valid-parentheses/

Given A string containing just the characters ‘(‘ ‘)‘ and, find the length of the longest valid (well-formed) parenthe SES substring.

"(()"for, the longest valid parentheses substring "()" are, which has length = 2.

Another example ")()())" is, where the longest valid parentheses substring "()()" are, which has length = 4.

Problem Solving Ideas:

This is a more difficult topic, I think for a while did not do it, referring to other users of the solution.

This problem is similar to valid parentheses done before, using the data results of the stack. The idea is that the opening parenthesis push is encountered, the closing parenthesis POPs, and if the stack is empty at this point false, or the entire string traversal ends, the stack has elements that are false.

This question is similar to the above, but the key is, how to record the longest length? For example, "() (())", traversing to the last one), how do you know if the length is from the first position or the third position?

Here are two very ingenious solutions.

First, a start variable is used to maintain the longest length on the current position and possibly the left edge. This boundary occurs only when a closing parenthesis is encountered and the opening parenthesis inside the stack pops up and the stack is empty. Why? Because this indicates that the pairing is now complete, from start to the current I, can be connected to a piece, there is no space between. So this is the length is I-start + 1.

Second, when the left parenthesis push is encountered, it is not going to be ' (' into the stack, but to push his subscript. If the stack is not empty, it will be possible to record the length of the current pair with I-stack.peak () after a closing parenthesis is encountered and the left parenthesis inside the stack is popped. In fact, this stack.peek () is the previous subscript that pops up the left parenthesis, so the length doesn't need to be 1.

If you meet a closing parenthesis, and the stack is empty? This means that the right parenthesis is simply superfluous and cannot be found paired with the left parenthesis. It will block all subsequent pairings, so start must be set to i+1.

 Public classSolution { Public intlongestvalidparentheses (String s) {intLength = 0, maxLength = 0, start = 0;; Stack<Integer> stack =NewStack<integer>();  for(inti = 0; I < s.length (); i++) {            if(S.charat (i) = = ' (') {Stack.push (i); }            if(S.charat (i) = = ') ') {                if(Stack.empty ()) {Start= i + 1; } Else{stack.pop (); if(!Stack.empty ()) {Length= i-Stack.peek (); MaxLength=Math.max (length, maxLength); }Else{length= I-start + 1; MaxLength=Math.max (length, maxLength); }                }            }        }        returnmaxLength; }}

The idea of the problem is still very ingenious, jumping, and based on the usual solution, interview if encountered, or more can see the level.

Longest Valid parentheses

Contact Us

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.