Skip to content
Open
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions valid-parentheses/hozzijeong.js
Copy link
Contributor

Choose a reason for hiding this comment

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

안녕하세요! 해당 풀이도 좋지만 Hashmap를 사용하면 좀 더 간결하게 작성할 수 있을거 같아요!

/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
  const closeMap = {
    '(': ')',
    '{': '}',
    '[': ']'
  };

  const stack = [];

  for (const ch of s) {
    if (ch in closeMap) {
      stack.push(closeMap[ch]);        // 기대되는 닫는 괄호를 push
    } else {
      if (stack.pop() !== ch) return false; // 닫는 괄호면 바로 비교
    }
  }

  return stack.length === 0;
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
const smallBracket = ['(',')'];
const middleBracket = ['{','}'];
const largeBracket = ['[',']'];


const leftStack = [];

for(const char of s) {
if(char === smallBracket[0]){
leftStack.push(smallBracket[1])
}else if (char === middleBracket[0]){
leftStack.push(middleBracket[1])
}else if(char === largeBracket[0]){
leftStack.push(largeBracket[1])
}else{
const last = leftStack.pop();
if(char === smallBracket[1] && last !== char) return false;
if(char === middleBracket[1] && last !== char) return false;
if(char === largeBracket[1] && last !== char) return false;
}
}


return leftStack.length === 0
};