Skip to content

Commit f72bfa3

Browse files
committed
valid parentheses solution
1 parent 4039896 commit f72bfa3

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

valid-parentheses/limlimjo.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isValid = function (s) {
6+
// 괄호 관리 스택
7+
const stack = [];
8+
9+
// 여는 괄호, 닫는 괄호 매핑
10+
const brackets = { "(": ")", "{": "}", "[": "]" };
11+
12+
// for문 돌며 확인
13+
for (let i of s) {
14+
// 여는 괄호일 경우
15+
if (brackets[i]) {
16+
stack.push(brackets[i]);
17+
// 닫는 괄호일 경우
18+
} else if (i !== stack.pop()) {
19+
return false;
20+
}
21+
}
22+
return true;
23+
};
24+
25+
// 시간복잡도: O(n)
26+
// 공간복잡도: O(n)

0 commit comments

Comments
 (0)