forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhwanmini.js
More file actions
39 lines (32 loc) · 777 Bytes
/
Copy pathhwanmini.js
File metadata and controls
39 lines (32 loc) · 777 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
27
28
29
30
31
32
33
34
35
36
37
38
39
// 시간복잡도: O(n)
// 공간복잡도: O(n)
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
if (s.length % 2 !== 0) return false
const stack = []
const opener = {
"(" : ")",
"{": "}",
"[": "]"
}
for (let i = 0 ; i < s.length; i++) {
if (s[i] in opener) {
stack.push(s[i]);
} else {
if (opener[stack.at(-1)] === s[i]) {
stack.pop()
} else {
return false
}
}
}
return stack.length === 0
};
console.log(isValid("()")); // true
console.log(isValid("()[]{}")); // true
console.log(isValid("(]")); // false
console.log(isValid("([])")); // true
console.log(isValid("([}}])")); // true