原题网址:https://leetcode.com/problems/remove-invalid-parentheses/
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.
Note: The input string may contain letters other than the parentheses (
and )
.
Examples:
"()())()" -> ["()()()", "(())()"] "(a)())()" -> ["(a)()()", "(a())()"] ")(" -> [""]
方法一:广度优先搜索。
public class Solution {
public List removeInvalidParentheses(String s) {
Set visit = new HashSet<>();
List results = new ArrayList<>();
LinkedList queue = new LinkedList<>();
queue.add(s);
boolean succ = false;
while (!queue.isEmpty()) {
String q = queue.remove();
if (succ) match(q, results); else succ = match(q, visit, queue, results);
}
return results;
}
private void match(String s, List results) {
// System.out.printf("Try matching %s\n", s);
int match = 0;
for(int i=0; i visit, LinkedList queue, List results) {
// System.out.printf("Try matching %s\n", s);
int match = 0;
for(int i = 0; i < s.length(); i ++) {
if (s.charAt(i) == '(') match ++;
else if (s.charAt(i) == ')') match --;
if (match < 0) {
for(int j=0; j<=i; j++) {
if (s.charAt(j) != ')') continue;
String r = s.substring(0, j) + s.substring(j+1);
if (visit.contains(r)) continue;
visit.add(r);
queue.add(r);
}
// System.out.printf("Try matching %s fail, queue=%s\n", s, queue);
return false;
}
}
if (match == 0) {
results.add(s);
// System.out.printf("Try matching %s successfully\n", s);
return true;
}
match = 0;
for(int i = s.length()-1; i >= 0; i --) {
if (s.charAt(i) == ')') match ++;
else if (s.charAt(i) == '(') match --;
if (match < 0) {
for(int j=s.length()-1; j>=i; j--) {
if (s.charAt(j) != '(') continue;
String r = s.substring(0, j) + s.substring(j+1);
if (visit.contains(r)) continue;
visit.add(r);
queue.add(r);
}
// System.out.printf("Try matching %s fail, queue=%s\n", s, queue);
return false;
}
}
// System.out.printf("Try matching %s fail, queue=%s\n", s, queue);
return false;
}
}
public class Solution {
private Set visit = new HashSet<>();
private List results = new ArrayList<>();
private void removeLeft(String s) {
int match = 0;
for(int i=s.length()-1; i>=0; i--) {
if (s.charAt(i) == ')') match ++;
else if (s.charAt(i) == '(') match --;
if (match < 0) {
for(int j=s.length()-1; j>=i; j--) {
if (s.charAt(j) != '(') continue;
String r = s.substring(0, j) + s.substring(j+1);
if (visit.contains(r)) continue;
visit.add(r);
removeLeft(r);
}
return;
}
}
results.add(s);
}
private void removeRight(String s) {
int match = 0;
for(int i=0; i removeInvalidParentheses(String s) {
removeRight(s);
return results;
}
}
这道题我没有做出好的解法,参考:http://algobox.org/remove-invalid-parentheses/
精妙之处在于:使用lastj保存上一次发现不匹配的位置和上一次删除括号的位置。
但如何证明唯一性?这个我还没有搞懂。
public class Solution {
private void removeLeft(String s, int lastI, int lastJ, List rs) {
int match = 0;
for(int i=lastI; i=0; i--) {
if (s.charAt(i) == ')') match ++;
else if (s.charAt(i) == '(') match --;
if (match >= 0) continue;
for(int j=lastJ; j=i; j--) {
if (s.charAt(j) != '(') continue;
if (j==lastJ || s.charAt(j+1) != '(') removeLeft(s.substring(0, j) + s.substring(j+1, s.length()), i-1, j-1, rs);
}
return;
}
rs.add(s);
}
private void removeRight(String s, int lastI, int lastJ, List rs) {
int match = 0;
for(int i=lastI; i= 0) continue;
for(int j=lastJ; j<=i; j++) {
if (s.charAt(j) != ')') continue;
if (j==lastJ || s.charAt(j-1) != ')') removeRight(s.substring(0, j) + s.substring(j+1, s.length()), i, j, rs);
}
return;
}
removeLeft(s, s.length()-1, s.length()-1, rs);
}
public List removeInvalidParentheses(String s) {
List results = new ArrayList<>();
removeRight(s, 0, 0, results);
return results;
}
}
带注释:
public class Solution {
private void removeLeft(String s, int matchTo, int removeTo, List results) {
int matched = 0;
for(int m=matchTo; m>=0; m--) {
if (s.charAt(m) == ')') matched ++;
else if (s.charAt(m) == '(') matched --;
if (matched >= 0) continue;
for(int r=removeTo; r>=m; r--) {
//检查是左括号才能删除
if (s.charAt(r) != '(') continue;
if (r==removeTo || s.charAt(r+1) != '(') removeLeft(s.substring(0, r)+s.substring(r+1), m-1, r-1, results);
}
//如果本次有删除,则留待后面加入到results
return;
}
results.add(s);
}
private void removeRight(String s, int matchTo, int removeTo, List results) {
int matched = 0;
for(int m=matchTo; m= 0) continue;
for(int r=removeTo; r<=m; r++) {
//检查是右括号才能删除
if (s.charAt(r) != ')') continue;
if (r==removeTo || s.charAt(r-1) != ')') removeRight(s.substring(0, r)+s.substring(r+1), m, r, results);
}
//如果本次有删除,则留待后面加入到results
return;
}
removeLeft(s, s.length()-1, s.length()-1, results);
}
public List removeInvalidParentheses(String s) {
List results = new ArrayList<>();
removeRight(s, 0, 0, results);
return results;
}
}