题解 | #合法的括号字符串#

合法的括号字符串

https://www.nowcoder.com/practice/eceb50e041ec40bd93240b8b3b62d221

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param s string字符串 
 * @return bool布尔型
 */
function isValidString( s ) {
    // write code here
   
    const reg = /(\()\*{0,}(\))/;
    s = s.trim()
    while(reg.test(s)){
        s = s.replace(reg,(a)=>{
            return a.replace("(","").replace(")","")
        })
         console.log('s===',s)
    }
    
    // 此时只剩下单方向的括号或者* v (*****  *)*.   *)**)**
  
    while(s.includes("(*") || s.includes("*)")){
    s = s.replace("(*","");
    s = s.replace("*)","")

    }
    s = s.replaceAll("*","");
    console.log(s)
    if(s.length) return false

     return true
}
module.exports = {
    isValidString : isValidString
};

全部评论
匹配法
点赞 回复 分享
发布于 2024-07-17 11:56 浙江

相关推荐

争当牛马还争不上
码农索隆:1.把简历改哈 2.猛投,狠投 3.把基础打牢 这样你在有机会的时候,才能抓住
点赞 评论 收藏
分享
05-29 09:02
门头沟学院 Java
点赞 评论 收藏
分享
05-24 14:12
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务