题解 | #合法的括号字符串#
合法的括号字符串
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 };