LC 20. Valid Parenttheses

/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
  const n = s.length
  if (n % 2 === 1) return false
  const pairs = new Map([
    [')', '('],
    [']', '['],
    ['}', '{']
  ])
  const stk = []
  for (let ch of s) {
    if (pairs.has(ch)) {
      if (!stk.length || stk[stk.length - 1] !== pairs.get(ch)) return false
      stk.pop()
    } else {
      stk.push(ch)
    }
  }
  return !stk.length
};
欢迎通过「邮件」或者点击「这里」告诉我你的想法
Welcome to tell me your thoughts via "email" or click "here"