-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestValidParentheses.java
More file actions
44 lines (41 loc) · 1.46 KB
/
Copy pathLongestValidParentheses.java
File metadata and controls
44 lines (41 loc) · 1.46 KB
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
import java.util.Stack;
/**
* Longest Valid Parantheses
*
* 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.
*
* @author joshluo
*
*/
public class LongestValidParentheses {
public int longestValidParentheses(String s) {
// Start typing your Java solution below
// DO NOT write main() function
Stack<Integer> lefts = new Stack<Integer>();
int maxLen = 0, last = -1; // last record the beginning of the valid string
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
lefts.push(i);
} else {
if (lefts.isEmpty()) {
last = i; // avoid all the useless "("
} else {
lefts.pop();
if (lefts.isEmpty()) { // there is a valid match from the last beginning
maxLen = Math.max(maxLen, i - last);
} else { // The valid match is from where the left '(' begins
maxLen = Math.max(maxLen, i - lefts.peek());
}
}
}
}
return maxLen;
}
public static void main(String args[]) {
}
}