Skip to content

Commit

Permalink
Valid Parentheses 풀이
Browse files Browse the repository at this point in the history
  • Loading branch information
BangDori committed Nov 17, 2024
1 parent ad6c6b9 commit 10e7f9b
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions bangdori/Valid Parentheses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const bracketMap = {
"]": -1,
")": -2,
"}": -3,
"[": 1,
"(": 2,
"{": 3,
};

var isValid = function (s) {
if (bracketMap[s[0]] < 0) {
return false;
}

const stack = [];

for (let i = 0; i < s.length; i += 1) {
const symbol = bracketMap[s[i]];

if (isOpenBracket(symbol)) {
stack.push(symbol);
continue;
}

if (isEmptyStack(stack)) {
return false;
}

if (stack[stack.length - 1] === symbol * -1) {
stack.pop();
} else {
stack.push(symbol);
}
}

return isEmptyStack(stack);
};

function isEmptyStack(stack) {
return stack.length === 0;
}

function isOpenBracket(symbol) {
return symbol > 0;
}

0 comments on commit 10e7f9b

Please sign in to comment.