Skip to content

Commit 9f8ed17

Browse files
committed
feat: 문제풀이 추가
1 parent f50fe1f commit 9f8ed17

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

valid-parentheses/hwanmini.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
} else {
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

Comments
 (0)