/
Solution.java
60 lines (52 loc) · 1.76 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Time : O(N); Space: O(N)
* @tag : Dynamic Programming; String
* @by : Steven Cooks
* @date: May 6, 2015
*************************************************************************
* 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.
*
*************************************************************************
* {@link https://leetcode.com/problems/longest-valid-parentheses/ }
*/
package _032_LongestValidParentheses;
/** see test {@link _032_LongestValidParentheses.SolutionTest } */
public class Solution {
// dynamic programming version
public int longestValidParentheses(String s) {
int len = s.length();
if (len <= 1) {
return 0;
}
int result = 0;
int local = 0;
// longest[i] = longest valid parentheses ending at s[i - 1]
int[] longest = new int[len + 1];
longest[0] = 0;
int lefts = 0;
for (int i = 0; i < len; i++) {
if (s.charAt(i) == '(') {
lefts++;
local = 0;
longest[i + 1] = 0;
} else if (lefts > 0) {
// ch == ')' and has open bracket for matching
lefts--;
// 2 for matching
local = 2 + local + longest[i + 1 - local - 2];
longest[i + 1] = local;
// update global optimization
result = Math.max(result, local);
}
}
return result;
}
}