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())()"]
")(" -> [""]
 1 public class Solution {
 2     private List<String> res = new ArrayList<String>();
 3     private int max = 0;
 4     public List<String> removeInvalidParentheses(String s) {
 5         dfs(s, "", 0, 0);
 6         if (res.size() == 0) {
 7             res.add("");
 8         }
 9
10         return res;
11     }
12
13     private void dfs(String str, String subRes, int countLeft, int maxLeft) {
14         if (countLeft > str.length()) return;
15         if (maxLeft + str.length() / 2 < max) return;
16         if (str.length() == 0) {
17             if (countLeft == 0 && subRes.length() != 0) {
18                 if (maxLeft > max) {
19                     max = maxLeft;
20                 }
21                 if (max == maxLeft && !res.contains(subRes)) {
22                     res.add(subRes.toString());
23                 }
24             }
25             return;
26         }
27
28         if (str.charAt(0) == ‘(‘) {
29             dfs(str.substring(1), subRes.concat("("), countLeft + 1, maxLeft + 1);
30             dfs(str.substring(1), subRes, countLeft, maxLeft);
31         } else if (str.charAt(0) == ‘)‘) {
32             if (countLeft > 0) {
33                 dfs(str.substring(1), subRes.concat(")"), countLeft - 1, maxLeft);
34             }
35             dfs(str.substring(1), subRes, countLeft, maxLeft);
36         } else {
37             dfs(str.substring(1), subRes.concat(String.valueOf(str.charAt(0))), countLeft, maxLeft);
38         }
39     }
40 }
时间: 2024-12-13 07:43:21

Remove Invalid Parentheses的相关文章

leetcode Remove Invalid Parentheses

题目连接 https://leetcode.com/problems/remove-invalid-parentheses/ Remove Invalid Parentheses Description Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may co

[LeetCode][JavaScript]Remove Invalid Parentheses

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: "()())()"

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: "()())()" -> ["()()()",

Remove Invalid Parentheses 解答

Question 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: "()())()" -> ["()()(

【Leetcode】Remove Invalid Parentheses

题目链接: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 parent

[LeetCode] 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: "()())()" -> ["()()()",

[leetcode] 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: "()())()" -> ["()()()",

[LeetCode] 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:"()())()" -> ["()()()", &

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 ). Example 1: Input: "()())()" Output: [&quo