题解 | #牛群的秘密通信#
牛群的秘密通信
https://www.nowcoder.com/practice/f0047999594d4cd39f85d7347c6941af
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return bool布尔型 */ function pair(a,b){ if((a==='('&&b===')')||(a==='{'&&b==='}')||(a==='['&&b===']')){ return true }else{ return false } } function is_valid_cow_communication( s ) { // write code here if(s.length==0){ return true } let stack = [] stack.push(s[0]) for(let i=1;i<s.length;i++){ if(pair(stack[stack.length-1],s[i])){ stack.pop() }else{ stack.push(s[i]) } } return stack.length==0 } module.exports = { is_valid_cow_communication : is_valid_cow_communication };