Leetcode Difficulty——Delete Invalid Brackets (Super Detailed Ideas)

(A very simple question, without difficulty, may test the programming ability, whether you can finish typing the code in a short time, I am a rookie)

Title:
Given you a string s consisting of several brackets and letters, delete the minimum number of invalid brackets to make the input string valid.
Return all possible results. Answers can be returned in any order.

Solution:
First, take a look at the data range s that contains at most 20 brackets. As soon as I see this 20, I immediately enumerate all the states in binary without thinking, and then write to the end. To sort, may timeout

But at the time, I thought that I might have to go through "whether it is a valid string" and "whether it is the longest string", and I felt that after two screenings, it should not time out
(I don't even think of any example to make him time out, but it does over)

code show as below:

class Solution {
public:
    bool test(string s) {
        int flag = 0;
        for(int i = 0; i < s.size(); i++) {
            if(s[i] == '(') flag++;
            else if(s[i] == ')' && flag != 0) flag--;
            else if(s[i] == ')' && flag == 0) return false;
        }
        if(flag == 0) return true;
        else return false;
    }
    vector<pair<int, string> > ddd;
    vector<string> res;
    vector<string> removeInvalidParentheses(string s) {
        vector<pair<bool, int> > flag;
        for(int i = 0; i < s.size(); i++) {
            if(s[i] == '(') flag.push_back(make_pair(true, i));
            if(s[i] == ')') flag.push_back(make_pair(false, i));
        }
        for(int i = 0; i < (1 << flag.size()); i++) {
            int f = 0;
            string temp = "";
            for(int j = 0; j < flag.size(); j++) {
                while(f < flag[j].second) {
                    temp = temp + s[f];
                    f++;
                }
                f++;
                if(((i >> j) & 1) != 1) continue;
                if(flag[j].first) temp = temp + '(';
                else temp = temp + ')';
            }
            while(f < s.size()) {
                temp = temp + s[f];
                f++;
            }
            if(test(temp)) ddd.push_back(make_pair(temp.size(), temp));
        }
        int f = 0;
        for(int i = 0; i < ddd.size(); i++) {
            f = max(f, ddd[i].first);
        }
        for(int i = 0; i < ddd.size(); i++) {
            if(ddd[i].first == f) {
                res.push_back(ddd[i].second);
            }
        }
        sort(res.begin(), res.end());
        res.erase(unique(res.begin(), res.end()), res.end());
        return res;
    }
};

Then there is no way, but to avoid deduplication, because if "((((", we remove two from it, and only get one case "(((", and a bunch of cases in binary enumeration, it is inevitable Deduplication, so we perform dfs, and treat the same parentheses that appear together as a group, such as "(((((", as a kind, when recursive, only consider 6 times, "", "(",..., " (((((", so that you don’t need to deduplicate, you can pass

class Solution {
public:
    bool test(string s) {
        int flag = 0;
        for(int i = 0; i < s.size(); i++) {
            if(s[i] == '(') flag++;
            else if(s[i] == ')' && flag != 0) flag--;
            else if(s[i] == ')' && flag == 0) return false;
        }
        if(flag == 0) return true;
        else return false;
    }
    vector<pair<bool, int> > flag; // 记录连续的 '(' 或 ')' 出现的次数
    string str; // 把字符串 s 存入全局变量
    vector<string> solve(int a, int v, int p) {
        vector<string> res;
        string t = "";
        for(int i = 0; i < v; i++) {
            if(flag[a].first) t = t + '(';
            else t = t + ')';
        }
        p = p + flag[a].second;
        while(p < str.size() && str[p] != '(' && str[p] != ')') {
            t = t + str[p];
            p++;
        }
        if(a == flag.size() - 1) {
            res.push_back(t);
            return res;
        }
        vector<string> temp;
        for(int i = 0; i <= flag[a + 1].second; i++) {
            temp = solve(a + 1, i, p);
            for(int j = 0; j < temp.size(); j++) {
                res.push_back(t + temp[j]);
            }
        }
        return res;
    }
    vector<string> removeInvalidParentheses(string s) {
        vector<string> res;
        vector<string> ddd;
        str = s;
        int p = 0;
        while(p < s.size()) {
            int f = 0;
            if(p < s.size() && s[p] == '(') {
                while(p < s.size() && s[p] == '(') {
                    p++;
                    f++;
                }
                flag.push_back(make_pair(true, f));
            }
            f = 0;
            if(p < s.size() && s[p] == ')') {
                while(p < s.size() && s[p] == ')') {
                    p++;
                    f++;
                }
                flag.push_back(make_pair(false, f));
            }
            while(p < s.size() && s[p] != '(' && s[p] != ')') {
                p++;
            }
        }
        if(flag.size() == 0) {
            res.push_back(s);
            return res;
        }
        vector<string> temp;
        string t = "";
        p = 0;
        while(p < str.size() && str[p] != '(' && str[p] != ')') {
            t = t + str[p];
            p++;
        }
        for(int i = 0; i <= flag[0].second; i++) { // 遍历每组括号可以存在的括号数量
            temp = solve(0, i, p);
            for(int j = 0; j < temp.size(); j++) {
                if(test(temp[j])) ddd.push_back(t + temp[j]);
            }
        }
        int f = 0;
        for(int i = 0; i < ddd.size(); i++) {
        	// 注意字符串的 size() 得到的不是 int 类型,需要强转
            f = max(int(ddd[i].size()), f);
        }
        for(int i = 0; i < ddd.size(); i++) {
            if(ddd[i].size() == f) {
                res.push_back(ddd[i]);
            }
        }
        return res;
    }
};

However, this time complexity is still not optimal, we can prune, because in fact, we know that we need to remove several parentheses (traversing the original string), so we can pass one more parameter to dfs , used to limit the total number of parentheses removed, stop traversal when it is 0, so that it can be optimized, and the code is too lazy to write

Guess you like

Origin blog.csdn.net/m0_52212261/article/details/128887441