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
38 changes: 38 additions & 0 deletions valid-parentheses/yuhyeon99.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
var stack = [];

var openDic = {
'{': '}',
'[': ']',
'(': ')'
};
var closeDic = {
'}': '{',
']': '[',
')': '('
};

for(let e of [...s]) {
if(!stack.length) {
stack.push(e);
continue;
};

const lastEle = stack.pop();

if(closeDic[e]) {
if(!openDic[lastEle]) return false;
if(openDic[lastEle] !== e) return false;
continue;
} else {
stack.push(lastEle);
stack.push(e);
}
}

return stack.length === 0;
};