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
};



如希望撰写评论,请发邮件至 me@tianhegao.com (直接点击邮箱可自动跳转至默认邮箱App,并填写收信人和邮件主题)或者点击这里在线留言,我会挑选对读者有价值的评论附加到文章末尾。



可通过以下渠道赞赏此文