[LeetCode] 1249. Minimum Remove to Make Valid Parentheses 移除无效的括号


Given a string s of '(' , ')' and lowercase English characters.

Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string.

Formally, a parentheses string is valid if and only if:

  • It is the empty string, contains only lowercase characters, or
  • It can be written as AB (A concatenated with B), where A and B are valid strings, or
  • It can be written as (A), where A is a valid string.

Example 1:

Input: s = "lee(t(c)o)de)"
Output: "lee(t(c)o)de"
Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.

Example 2:

Input: s = "a)b(c)d"
Output: "ab(c)d"

Example 3:

Input: s = "))(("
Output: ""
Explanation: An empty string is also valid.

Example 4:

Input: s = "(a(b(c)d)"
Output: "a(b(c)d)"

Constraints:

  • 1 <= s.length <= 105
  • s[i] is either'(' , ')', or lowercase English letter.

这道题給了一个有括号的字符串,说是尽可能的少移除括号的个数,使得整个字符串变得合法,让返回这个合法的字符串。LeetCode 有很多关于括号的题目,大部分的解题思路都很像,都是从左右括号的个数和位置入手,若只是想知道字符串是否合法,只需要统计左右括号的个数,任何时候右括号的个数都不能超过左括号的个数。而这道题要移除非法的括号,所以位置信息也很重要。这里使用一个栈 stack 来记录左括号的位置,遍历给定字符串s,若遇到了左括号,则将当前位置压入栈,若遇到右括号,则判断,若当前栈为空,说明前面没有左括号了,则当前右括号就是非法的,标记当前位置为星号,若栈不为空,则移除栈顶元素,即移除一个左括号。这样操作完成了之后,所有的非法右括号的位置都被标记了,而此时残留在栈中的左括号也都是非法的,将其对应位置标记为星号。最后只要移除所有的星号,得到的就是合法的字符串了,参见代码如下:


解法一:

class Solution {
public:
    string minRemoveToMakeValid(string s) {
        string res;
        stack<int> st;
        for (int i = 0; i < s.size(); ++i) {
            if (s[i] == '(') st.push(i);
            else if (s[i] == ')') {
                if (st.empty()) s[i] = '*';
                else st.pop();
            }
        }
        while (!st.empty()) {
            s[st.top()] = '*'; st.pop();
        }
        for (char c : s) {
            if (c != '*') res += c;
        }
        return res;
    }
};

我们也可以优化一下空间复杂度,不用栈了,而是用两个变量 left 和 right,分别表示左右括号的个数,先遍历一遍给定字符串s,统计出所有的右括号的个数。然后再次遍历给定字符串s,若遇左扩号了,判断若此时 left 和 right 相等了,说明后面没有多余的右括号了,此时的左括号就是非法的,则直接跳过,否则就让 left 自增1。若遇到右括号了,则 right 先自减1,因为 right 表示的是后面还有的右括号的个数,若此时 left 等于0了,说明前面没有对应的左括号了,则直接跳过,否则 left 自减1。对于所有没有 continue 的情况,则均加入到结果 res 中,表示对应的字母,或者左右括号就是合法的,参见代码如下:


解法二:

class Solution {
public:
    string minRemoveToMakeValid(string s) {
        string res;
        int left = 0, right = 0;
        for (char c : s) {
            if (c == ')') ++right;
        }
        for (char c : s) {
            if (c == '(') {
                if (left == right) continue;
                ++left;
            } else if (c == ')') {
                --right;
                if (left == 0) continue;
                --left;
            }
            res += c;
        }
        return res;
    }
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/1249


类似题目:

Minimum Number of Swaps to Make the String Balanced


参考资料:

https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/

https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/discuss/419402/JavaC%2B%2B-Stack

https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/discuss/419466/Constant-Space-Solution


LeetCode All in One 题目讲解汇总(持续更新中...)

posted @ 2021-11-06 09:29  Grandyang  阅读(1535)  评论(1编辑  收藏  举报
Fork me on GitHub