Skip to content

Commit

Permalink
valid parentheses solution
Browse files Browse the repository at this point in the history
  • Loading branch information
limlimjo committed Jan 14, 2025
1 parent 4039896 commit f72bfa3
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions valid-parentheses/limlimjo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function (s) {
// 괄호 관리 스택
const stack = [];

// 여는 괄호, 닫는 괄호 매핑
const brackets = { "(": ")", "{": "}", "[": "]" };

// for문 돌며 확인
for (let i of s) {
// 여는 괄호일 경우
if (brackets[i]) {
stack.push(brackets[i]);
// 닫는 괄호일 경우
} else if (i !== stack.pop()) {
return false;
}
}
return true;
};

// 시간복잡도: O(n)
// 공간복잡도: O(n)

0 comments on commit f72bfa3

Please sign in to comment.