删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果。
说明: 输入可能包含了除 ( 和 ) 以外的字符。
示例 1:
输入: "()())()"
输出: ["()()()", "(())()"]
示例 2:
输入: "(a)())()"
输出: ["(a)()()", "(a())()"]
示例 3:
输入: ")("
输出: [""]
var isValid = function(s) {
let openParenthes = 0;
for(let i = 0; i < s.length; i++) {
if (s[i] === '(') {
openParenthes++;
} else if (s[i] === ')') {
if (openParenthes === 0) return false;
openParenthes--;
}
}
return openParenthes === 0;
};
/**
* @param {string} s
* @return {string[]}
*/
var removeInvalidParentheses = function(s) {
if (!s || s.length === 0) return [""];
const ret = [];
const queue = [s];
const visited = {};
let current = null;
let removedParentheses = 0; // 只记录最小改动
while ((current = queue.shift())) {
let hit = isValid(current);
if (hit) {
if (!removedParentheses) {
removedParentheses = s.length - current.length
}
if (s.length - current.length > removedParentheses) return ret.length === 0 ? [""] : ret;;
ret.unshift(current);
continue;
}
for (let i = 0; i < current.length; i++) {
if (current[i] !== ')' && current[i] !== '(') continue;
const subString = current.slice(0, i).concat(current.slice(i + 1));
if (visited[subString]) continue;
visited[subString] = true;
queue.push(subString);
}
}
return ret.length === 0 ? [""] : ret;
};