跳到主要内容

Longest Valid Parentheses

描述

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.

分析

使用栈

# Longest Valid Parenthese
# Using a stack
# Time Complexity: O(n), Space Complexity: O(n)
class Solution:
def longestValidParentheses(self, s: str) -> int:
max_len = 0
stack = deque()
stack.append(-1)

for i in range(len(s)):
if s[i] == '(':
stack.append(i);
else:
stack.pop()
if len(stack) == 0:
stack.append(i)
else:
max_len = max(max_len, i - stack[-1])

return max_len

Dynamic Programming, One Pass

// Longest Valid Parenthese
// 时间复杂度O(n),空间复杂度O(n)
// @author 一只杰森(http://weibo.com/wjson)
class Solution {
public int longestValidParentheses(final String s) {
int[] f = new int[s.length()];
int result = 0;
for (int i = s.length() - 2; i >= 0; --i) {
int match = i + f[i + 1] + 1;
// case: "((...))"
if (s.charAt(i) == '(' && match < s.length() &&
s.charAt(match) == ')') {
f[i] = f[i + 1] + 2;
// if a valid sequence exist afterwards "((...))()"
if (match + 1 < s.length()) f[i] += f[match + 1];
}
result = Math.max(result, f[i]);
}
return result;
}
}

两遍扫描

// Longest Valid Parenthese
// 两遍扫描,时间复杂度O(n),空间复杂度O(1)
// @author 曹鹏(http://weibo.com/cpcs)
class Solution {
public int longestValidParentheses(final String s) {
int result = 0, depth = 0, start = -1;
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) == '(') {
++depth;
} else {
--depth;
if (depth < 0) {
start = i;
depth = 0;
} else if (depth == 0) {
result = Math.max(result, i - start);
}
}
}

depth = 0;
start = s.length();
for (int i = s.length() - 1; i >= 0; --i) {
if (s.charAt(i) == ')') {
++depth;
} else {
--depth;
if (depth < 0) {
start = i;
depth = 0;
} else if (depth == 0) {
result = Math.max(result, start - i);
}
}
}
return result;
}
}

相关题目