class Solution {
private int len;
private char[] charArray;
private Set<String> validExpressions = new HashSet<>();
public List<String> removeInvalidParentheses(String s) {
this.len = s.length();
this.charArray = s.toCharArray();
int leftRemove = 0;
int rightRemove = 0;
for (char ch : charArray) {
if (ch == '(') {
leftRemove++;
} else if (ch == ')') {
if (leftRemove == 0) {
rightRemove++;
} else if (leftRemove > 0) {
leftRemove--;
}
}
}
StringBuilder path = new StringBuilder();
dfs(0, 0, 0, leftRemove, rightRemove, path);
return new ArrayList<>(this.validExpressions);
}
public void dfs(int index, int leftCount, int rightCount, int leftRemove, int rightRemove, StringBuilder path) {
if (index == len) {
if (leftRemove == 0 && rightRemove == 0) {
validExpressions.add(path.toString());
}
return;
}
char character = charArray[index];
if (character == '(' && leftRemove > 0) {
dfs(index + 1, leftCount, rightCount, leftRemove - 1, rightRemove, path);
}
if (character == ')' && rightRemove > 0) {
dfs(index + 1, leftCount, rightCount, leftRemove, rightRemove - 1, path);
}
path.append(character);
if (character != '(' && character != ')') {
dfs(index + 1, leftCount, rightCount, leftRemove, rightRemove, path);
} else if (character == '(') {
dfs(index + 1, leftCount + 1, rightCount, leftRemove, rightRemove, path);
} else if (rightCount < leftCount) {
dfs(index + 1, leftCount, rightCount + 1, leftRemove, rightRemove, path);
}
path.deleteCharAt(path.length() - 1);
}
}
评论