-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathremove-invalid-parenthesis.cpp
92 lines (84 loc) · 2.43 KB
/
remove-invalid-parenthesis.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* @file remove-invalid-parenthesis.cpp
* @author prakash (prakashsellathurai@gmail.com)
* @brief
Given a string s that contains parentheses and letters, remove the minimum
number of invalid parentheses to make the input string valid.
(https://leetcode.com/problems/remove-invalid-parentheses/)
* @version 0.1
* @date 2021-08-13
*
* @copyright Copyright (c) 2021
*
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
private:
vector<string> ans;
vector<int> delLeft;
void getAns(string &s, string cur, int pos, int cntl, int cntr, int cnt) {
if(cntr == 0 && cntl == 0)
ans.push_back(cur + s.substr(pos));
else {
for (int tail = s.length() - cntl - cntr; pos <= tail; ++pos) {
if (s[pos] == ')') {
if (cntr && (cur.empty() || cur.back() != ')'))
getAns(s, cur, pos + 1, cntl, cntr - 1, cnt);
if (cnt == 0)
return;
--cnt;
cur += ')';
} else if (s[pos] == '(') {
if (cntr == 0 && cntl > delLeft[pos] &&
(cur.empty() || cur.back() != '('))
getAns(s, cur, pos + 1, cntl - 1, cntr, cnt);
++cnt;
cur += '(';
} else
cur += s[pos];
}
}
}
public:
vector<string> removeInvalidParentheses(string s) {
// edge case
if (s == "")
return vector<string>(1, "");
// finding the number of opening brackets to be deleted with presum technique
int cntr = 0;
delLeft.resize(s.length());
delLeft[s.length() - 1] = 0;
for (int i = s.length() - 1; i > 0; --i) {
if (s[i] == ')')
++cntr, delLeft[i - 1] = delLeft[i];
else if (s[i] == '(') {
if (cntr)
--cntr, delLeft[i - 1] = delLeft[i];
else
delLeft[i - 1] = delLeft[i] + 1;
} else
delLeft[i - 1] = delLeft[i];
}
ans.resize(0);
// cntr hold how many extra closing brackets are there
if (s[0] == '(' && cntr == 0)
getAns(s, "", 0, delLeft[0] + 1, cntr, 0);
else if (s[0] == '(')
getAns(s, "", 0, delLeft[0], cntr - 1, 0);
else if (s[0] == ')')
getAns(s, "", 0, delLeft[0], cntr + 1, 0);
else
getAns(s, "", 0, delLeft[0], cntr, 0);
return ans;
}
};
int main(int argc, const char **argv) {
vector<string> results = Solution().removeInvalidParentheses("()())()");
for (auto result : results) {
cout << result << endl;
}
return 0;
}