1var removeInvalidParentheses = function (s) {2const isValid = (s) => {3const stack = [];4for (let c of s) {5if (c == "(") stack.push(c);6else if (c == ")") {7if (stack.length && stack.at(-1) == "(") stack.pop();8else return false;9}10}11return stack.length == 0;12};1314const Q = [s],15vis = new Set([s]),16ans = [];17let found = false;18while (Q.length) {19const top = Q.shift();20if (isValid(top)) {21ans.push(top);22found = true;23}24if (found) continue;25for (let i = 0; i < top.length; i++) {26if (!"()".includes(top[i])) continue;2728const str = top.slice(0, i) + top.slice(i + 1);29if (!vis.has(str)) {30Q.push(str);31vis.add(str);32}33}34}35if (ans.length == 0) return [""];36return ans;37};