-
Notifications
You must be signed in to change notification settings - Fork 43
/
ValidParentheses.java
172 lines (146 loc) · 4.81 KB
/
ValidParentheses.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package LeetCodeJava.Stack;
// https://www.tutorialspoint.com/java/util/stack_pop.htm
// https://leetcode.com/problems/valid-parentheses/
// https://www.softwaretestinghelp.com/java-queue-interface/
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
public class ValidParentheses {
// V0
// IDEA : STACK + DICT
public boolean isValid(String s) {
if (s == null || s.length() == 0){
return true;
}
Map<String, String> map = new HashMap<>();
map.put("(", ")");
map.put("{", "}");
map.put("[", "]");
/** NOTE !!! use stack here, FILO */
Stack<String> stack = new Stack<>();
for (String x : s.split("")){
//System.out.println("x = " + x + " stack = " + stack);
if (map.containsKey(x)){
stack.add(x);
}else{
if (stack.isEmpty()){
return false;
}
String last = stack.pop();
if (!map.get(last).equals(x)){
return false;
}
}
}
return stack.isEmpty();
}
// V0'
// IDEA : STACK + DICT
public boolean isValid_0(String s) {
if (s.length() % 2 != 0){
return false;
}
HashMap<String, String> map = new HashMap<>();
Stack st = new Stack();
map.put("(", ")");
map.put("{", "}");
map.put("[", "]");
for (int i = 0; i < s.length(); i++){
String cur = String.valueOf(s.charAt(i));
if (st.empty() && !map.containsKey(cur)){
return false;
}
if (map.containsKey(cur)){
st.push(cur);
}
else{
String popElement = (String) st.pop();
String expect = map.get(popElement);
if (!expect.equals(cur)){
return false;
}
}
}
return true ? st.empty() : false;
}
// V0'
// IDEA : STACK + add "inverse" Parentheses to stack directly
// https://www.bilibili.com/video/BV1AF411w78g/?share_source=copy_web&vd_source=771d0eba9b524b4f63f92e37bde71301
public boolean isValid_(String s) {
if (s.length() % 2 != 0){
return false;
}
Stack st = new Stack();
for (int i = 0; i < s.length(); i++){
String cur = String.valueOf(s.charAt(i));
if (cur.equals("(")){
st.push(")");
continue;
}
else if (cur.equals("{")){
st.push("}");
continue;
}
else if (cur.equals("[")){
st.push("]");
continue;
}
else{
if (st.empty()){
return false;
}else{
String _cur = (String) st.pop();
if (!_cur.equals(cur)){
return false;
}
}
}
}
return true ? st.empty() : false;
}
// V1
// https://leetcode.com/problems/valid-parentheses/solutions/3398779/python-java-c-simple-solution-video-solution/
public boolean isValid_1(String s) {
Stack<Character> stack = new Stack<>();
for (char c : s.toCharArray()) {
if (c == '(' || c == '{' || c == '[') {
stack.push(c);
} else {
if (stack.empty()) {
return false;
}
if (c == ')' && stack.peek() == '(') {
stack.pop();
} else if (c == '}' && stack.peek() == '{') {
stack.pop();
} else if (c == ']' && stack.peek() == '[') {
stack.pop();
} else {
return false;
}
}
}
return stack.empty();
}
// V2
// https://leetcode.com/problems/valid-parentheses/solutions/3399077/easy-solutions-in-java-python-and-c-look-at-once-with-exaplanation/
public boolean isValid_2(String s) {
Stack<Character> stack = new Stack<>();
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[') {
stack.push(s.charAt(i));
count++;
} else {
if (stack.isEmpty()) return false;
char ch = stack.pop();
if ((s.charAt(i) == ')' && ch == '(') || (s.charAt(i) == ']' && ch == '[') || (s.charAt(i) == '}' && ch == '{')) {
} else {
return false;
}
count--;
}
}
return count == 0;
}
}