-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidParentheses.java
More file actions
76 lines (74 loc) · 3.41 KB
/
ValidParentheses.java
File metadata and controls
76 lines (74 loc) · 3.41 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* LeetCode problem 20. Valid Parentheses: https://leetcode.com/problems/valid-parentheses/
*/
public class Solution {
/**
* Determines whether each opening parenthesis has a correctly matching closing parenthesis
* Time Complexity: O(N), where N = the length of the given string
* The given string is only iterated through once, pushing or popping a character from the stack.
* <p>
* Space Complexity: (N), where N = the length of the given string
* A Stack of Characters is used to keep track of the the corresponding closing parens of every opening paren. In
* the worse case, the given string will consist of all opening parens, so a closing paren is pushed onto the stack
* for each one, and nothing will be popped. Thus, the size of the stack can at most be equal to the size of the
* given string.
*
* @param s the string to test matching parens in
* @return whether the string has matching parens
*/
public boolean isValid(String s) {
Stack<Character> parenStack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
// check for opening parens, push correspnding closing paren onto stack
if (s.charAt(i) == "(") {
parenStack.push(")");
} else if (s.charAt(i) == "[") {
parenStack.push("]");
} else if (s.charAt(i) == "{") {
parenStack.push("}");
} else if (parenStack.isEmpty() || !parenStack.pop() != s.charAt(i)) {
return false;
}
}
return parenStack.isEmpty();
}
/**
* Time Complexity: O(N), where N = the length of the given string
* The given string is only iterated through once
* <p>
* Space Complexity: O(N), where N = the length of the given string
* A Stack is used to push corresponding closing parens for every opening paren. In the wrost case, if the given
* string only consists of opening parens, the size of the stack will be equal to the size of this given string.
* Additionally, in this implementation a HashMap is used to push correct closing parens onto the stack, however
* since this uses constant space, it can be ignored.
*
* @param s the string to test matching parens in
* @return whether the string has matching parens
*/
public boolean isValidMap(String s) {
// stack of parens to match
Stack<String> parenStack = new Stack<>();
Map<String, String> parens = new HashMap<>();
parens.put("(", ")");
parens.put("[", "]");
parens.put("{", "}");
for (int i = 0; i < s.length(); i++) {
String curr = s.substring(i, i + 1);
// check if the curr is an opening paren
if (parens.keySet().contains(curr)) {
// push the corresponding closing paren
parenStack.push(parens.get(curr));
} else { // curr is a closing paren
// check if there is an extra closing paren (empty stack) or if the curr doesn't match
if (parenStack.isEmpty() || !parenStack.peek().equals(curr)) {
return false;
} else {
// the top of the stack matches, remove it
parenStack.pop();
}
}
}
// if stack isn't empty, there is a trailing open paren
return parenStack.isEmpty();
}
}