-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL20.java
More file actions
45 lines (41 loc) · 1.38 KB
/
L20.java
File metadata and controls
45 lines (41 loc) · 1.38 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
45
import java.util.HashMap;
import java.util.Map;
public class L20 {
/**
* 20. Valid Parentheses https://leetcode.com/problems/valid-parentheses/
*
* @timeComplexity O(n)
* @spaceComplexity O(1) ignoring recursion stack
*/
static class Solution {
private static final Map<Character, Character> closing = new HashMap<Character, Character>() {{
put('{', '}');
put('(', ')');
put('[', ']');
}};
public boolean isValid(String s) {
if (s.equals("")) {
return true;
}
int closingIndex = findClosingIndex(s.charAt(0), s);
if (closingIndex == -1)
return false;
else
return isValid(s.substring(1, closingIndex)) && isValid(s.substring(closingIndex + 1));
}
private int findClosingIndex(char c, String s) {
if (s.charAt(0) != '(' && s.charAt(0) != '{' && s.charAt(0) != '[')
return -1;
int bal = 1; // Balance keeps track of opening braces seen so far
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == c)
bal++;
else if (s.charAt(i) == closing.get(c))
bal--;
if (bal == 0)
return i;
}
return -1;
}
}
}