Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions valid-parentheses/Cyjin-jani.js
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Stack / Priority Queue
  • 설명: 이 코드는 괄호의 유효성을 검사하기 위해 스택 자료구조를 사용하여 열린 괄호와 닫힌 괄호를 매칭하는 방식을 활용합니다. 스택은 괄호의 짝을 맞추는 데 적합한 구조입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// tc: O(n)
// sc: O(n)
const isValid = function (s) {
const bracketMap = {
'(': ')',
'{': '}',
'[': ']',
};

if (s.length % 2 !== 0 || isCloseBracket(s[0])) return false;

const stack = [];

for (let i = 0; i < s.length; i++) {
if (stack.length === 0) {
stack.push(s[i]);
continue;
}

let topBracket = stack.pop();
if (bracketMap[topBracket] !== s[i]) {
stack.push(topBracket);
stack.push(s[i]);
}
}

return stack.length === 0;
};

function isCloseBracket(char) {
const closeBrackets = [')', '}', ']'];

return closeBrackets.includes(char);
}
Loading