Skip to content

Commit f7961c5

Browse files
committed
solve: valid parentheses
1 parent b840ada commit f7961c5

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

valid-parentheses/wogha95.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* TC: O(S)
3+
* s 매개변수의 길이만큼 순회 1번
4+
*
5+
* SC: O(S)
6+
* 최악의 경우 S의 길이만큼 stack 배열에 모두 push 할 수 있기 때문에
7+
*
8+
* S: s.length
9+
*/
10+
11+
/**
12+
* @param {string} s
13+
* @return {boolean}
14+
*/
15+
var isValid = function (s) {
16+
const map = {
17+
"(": ")",
18+
"{": "}",
19+
"[": "]",
20+
};
21+
const stack = [];
22+
23+
// 1. s의 길이만큼 순회를 하면서
24+
for (const char of s) {
25+
// 2. 열린 괄호라면 stack에 짝지어진 닫힌 괄호를 저장
26+
// 3. 닫힌 괄호라면 stack에서 꺼낸 것과 동일한지 확인
27+
switch (char) {
28+
case "(":
29+
case "{":
30+
case "[":
31+
stack.push(map[char]);
32+
break;
33+
case "}":
34+
case ")":
35+
case "]":
36+
if (stack.pop() !== char) {
37+
return false;
38+
}
39+
}
40+
}
41+
42+
// 4. 남은 괄호가 없는지 확인
43+
return stack.length === 0;
44+
};

0 commit comments

Comments
 (0)