We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f50fe1f commit 9f8ed17Copy full SHA for 9f8ed17
valid-parentheses/hwanmini.js
@@ -0,0 +1,39 @@
1
+// 시간복잡도: O(n)
2
+// 공간복잡도: O(n)
3
+
4
+/**
5
+ * @param {string} s
6
+ * @return {boolean}
7
+ */
8
+var isValid = function(s) {
9
+ if (s.length % 2 !== 0) return false
10
11
+ const stack = []
12
13
+ const opener = {
14
+ "(" : ")",
15
+ "{": "}",
16
+ "[": "]"
17
+ }
18
19
+ for (let i = 0 ; i < s.length; i++) {
20
+ if (s[i] in opener) {
21
+ stack.push(s[i]);
22
+ } else {
23
+ if (opener[stack.at(-1)] === s[i]) {
24
+ stack.pop()
25
26
+ return false
27
28
29
30
31
32
+ return stack.length === 0
33
+};
34
35
+console.log(isValid("()")); // true
36
+console.log(isValid("()[]{}")); // true
37
+console.log(isValid("(]")); // false
38
+console.log(isValid("([])")); // true
39
+console.log(isValid("([}}])")); // true
0 commit comments