代码拉取完成,页面将自动刷新
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
/**
* 301. 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 _301 {
public static class Solution1 {
public List<String> removeInvalidParentheses(String s) {
List<String> result = new ArrayList<>();
if (s == null) {
return result;
}
Set<String> visited = new HashSet();
Queue<String> q = new LinkedList();
q.offer(s);
visited.add(s);
boolean found = false;
while (!q.isEmpty()) {
String curr = q.poll();
if (isValid(curr)) {
found = true;
result.add(curr);
}
if (found) {
continue;//this means if the initial input is already a valid one, we'll just directly return it and there's actually only one valid result
}
for (int i = 0; i < curr.length(); i++) {
if (curr.charAt(i) != '(' && curr.charAt(i) != ')') {
continue;//this is to rule out those non-parentheses characters
}
String next = curr.substring(0, i) + curr.substring(i + 1);
if (!visited.contains(next)) {
q.offer(next);
visited.add(next);
}
}
}
return result;
}
private boolean isValid(String str) {
char[] chars = str.toCharArray();
int count = 0;
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (c == '(') {
count++;
}
if (c == ')') {
count--;
if (count == -1) {
return false;
}
}
}
return count == 0;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。