알고리즘 7 - Valid Parentheses

박진현·2021년 7월 11일
0

Q.

Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.

Example 1:

Input: s = "()"
Output: true
Example 2:

Input: s = "()[]{}"
Output: true
Example 3:

Input: s = "(]"
Output: false
Example 4:

Input: s = "([)]"
Output: false
Example 5:

Input: s = "{[]}"
Output: true

Constraints:

1 <= s.length <= 104
s consists of parentheses only '()[]{}'.

a)

var isValid = function(s) {
    let pair = {
      '(' : ')',
      '{' : '}',
      '[' : ']'
    } ;
    const result = [];
    for(let i=0;i<s.length;i++) {
      if ( s[i] in pair) {
        result.push(s[i])
      }
      else if (pair[result.pop()] !== s[i]) {
        return false;
      }
    }
    return result.length === 0;
};

Runtime: 68 ms, faster than 96.05% of JavaScript online submissions for Valid Parentheses.
Memory Usage: 38.7 MB, less than 76.21% of JavaScript online submissions for Valid Parentheses.

profile
👨🏻‍💻 호기심이 많고 에러를 좋아하는 프론트엔드 개발자 박진현 입니다.

0개의 댓글