921. Minimum Add to Make Parentheses Valid [JavaScript]

一、题目

Given a string S of ‘(’ and ‘)’ parentheses, we add the minimum number of parentheses ( ‘(’ or ‘)’, and in any positions ) so that the resulting parentheses string is valid.

Formally, a parentheses string is valid if and only if:

  • It is the empty string, or
  • It can be written as AB (A concatenated with B), where A and B are valid strings, or
  • It can be written as (A), where A is a valid string.

Given a parentheses string, return the minimum number of parentheses we must add to make the resulting string valid.

二、题目大意

给定一个字符串S,它其中只包含’(‘和’)’,当字符中的所有的’(‘有相关联的’)’,那么这个字符串就是一个有效字符串,对于S可以采用添加’(‘或者’)'的操作使得其成为一个有效字符串的最小操作次数。

三、解题思路

读懂题意之后,很好解决这道题目,就是让我们去构造’()’,并且它是可以嵌套的。

而在字符串S中只有当左边有’(‘的情况,右边的’)‘才能形成一个有效的关联,所以只要统计出不能形成有效关联的’(‘和’)'的个数,即为题目结果。

四、代码实现
const minAddToMakeValid = (S) => {
  const max = S.length
  if (max === 0) {
    return 0
  }
  if (max === 1) {
    return 1
  }
  let i = 0 // 记录左边括号
  let j = 0 // 记录右边括号
  let start = 0
  while (start < max) {
    const item = S[start++]
    if (item === '(') {
      i++
    } else {
      if (i > 0) {
        // 消除
        i--
      } else {
        j++
      }
    }
  }
  return i + j
}

  如果本文对您有帮助,欢迎关注微信公众号,为您推送更多大前端相关的内容, 欢迎留言讨论,ε=ε=ε=┏(゜ロ゜;)┛。

  您还可以在这些地方找到我:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值