Leetcode:longest Valid parentheses O (n) time O (1) space

Source: Internet
Author: User

Topic:

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.

It means to find out the length of the longest brace match.


This problem also has a lot of reports, probably looked at a little, I see more is DP and use stack to do mark, all useful to O (N) space complexity. In fact, the spatial complexity of O (n) is unnecessary, and an O (n) time, O (1) space solution is proposed.

The main idea is that the positive sequence of each traversal of the "positive sequence needs to traverse, reverse order the worst need to traverse."

    • First, the positive
    1. Like (())) more than (the place is good to do, record the length of the previous match.)
    2. The problem is (() How to deal with (() where it is (always more), and how to find the strings that match the conditions. At this point, the reverse is needed to solve it, the actual is to eliminate the superfluous (.
    • Reverse
    1. Before the reverse traversal of the string (more than), this string will only have one and must be at the end. Because: if there is a string like this, it has the ability to traverse backwards, that is, it can accommodate (and can accommodate).
    2. In turn, from the beginning, the length of the previous match is recorded directly in the (more) place that is unqualified. In this process, obviously each one will have (to match, find the strings that match the conditions in the string.)
    • Finally, as many as possible (), that is, the end of the perfect match, the length of this time is also recorded, return the maximum length of the process.


The so-called record down is actually directly compared to the maximum length, updating the maximum length. The code is as follows:


public class Solution {public int longestvalidparentheses (String s) {int left = 0, len = 0, Tmpl = 0, pos =                -1;            for (int i = 0; I! = S.length (); i++) {if (S.charat (i) = = ' (') {left + +; Tmpl + +;                }else{left--;                    if (left = =-1) {if (Tmpl > len) {len = Tmpl; } left = 0;                      Tmpl = 0;                pos = i;                }else{Tmpl + +;            }}} if (Left > 0) {left = 0; tmpl = 0; for (int i = S.length ()-1; I! = pos; i--) {if (S.charat (i) = = ') ') {left + +; Tmpl +                +;                    }else{left--;                        if (left = =-1) {if (Tmpl > len) {len = Tmpl;                       } left = 0;                     Tmpl = 0;                    }else{Tmpl + +;        }}}} if (left = = 0) {if (Tmpl > len) len = Tmpl;    } return len; }}



Leetcode:longest Valid parentheses O (n) time O (1) space

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.