- Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathLongestValidParentheses.java
More file actions
Latest commit
26 lines (26 loc) · 797 Bytes
/
Copy pathLongestValidParentheses.java
File metadata and controls
26 lines (26 loc) · 797 Bytes
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
publicclassSolution {
publicbooleanisValid(Strings) {
Stack<Character> stack = newStack<Character>();
for (inti = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
stack.push('(');
} elseif (!stack.empty() && stack.peek() == '(') {
stack.pop();
} else {
returnfalse;
}
}
returnstack.empty();
}
publicintlongestValidParentheses(Strings) {
intmaxlen = 0;
for (inti = 0; i < s.length(); i++) {
for (intj = i + 2; j <= s.length(); j+=2) {
if (isValid(s.substring(i, j))) {
maxlen = Math.max(maxlen, j - i);
}
}
}
returnmaxlen;
}
}