-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestValidParentheses.java
55 lines (47 loc) · 1.13 KB
/
LongestValidParentheses.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
package string;
import java.util.*;
/**
* @author Shogo Akiyama
* Solved on 12/12/2019
*
* 32. Longest Valid Parentheses
* https://leetcode.com/problems/longest-valid-parentheses/
* Difficulty: Hard
*
* Approach: Stack
* Runtime: 5 ms, faster than 54.29% of Java online submissions for Longest Valid Parentheses.
* Memory Usage: 36.4 MB, less than 100.00% of Java online submissions for Longest Valid Parentheses.
*
* Time Complexity: O(n)
* Space Complexity: O(n)
* Where n is the length of a string
*
* @see StringTest#testLongestValidParentheses()
*/
public class LongestValidParentheses {
public int longestValidParentheses(String s) {
Stack<Integer> stack = new Stack<Integer>();
boolean[] valid = new boolean[s.length()];
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
stack.push(i);
} else {
if (!stack.isEmpty()) {
valid[stack.pop()] = true;
valid[i] = true;
}
}
}
int count = 0;
int max = 0;
for (int i = 0; i < valid.length; i++) {
if (valid[i]) {
count++;
max = Math.max(max, count);
} else {
count = 0;
}
}
return max;
}
}