Skip to content

Commit 5cdac14

Browse files
authored
Add C++ Solution of "minimum-remove-to-make-valid-parentheses" & Update README.md (codedecks-in#153)
* Update README.md * Create minimum-remove-to-make-valid-parentheses.cpp * edit wrong filename
1 parent a282cb8 commit 5cdac14

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed
File renamed without changes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
string minRemoveToMakeValid(string s) {
4+
stack<int> stack_for_index;//stack to save index of '('
5+
string result_string = "";
6+
7+
// count wrong parentheses with stack & make string
8+
for (int i = 0; i < s.size(); i++)
9+
{
10+
if (s[i] == '(') {
11+
stack_for_index.push(result_string.size());
12+
result_string.push_back(s[i]);
13+
}
14+
else if (s[i] == ')') {
15+
if (!stack_for_index.empty()) {
16+
stack_for_index.pop();
17+
result_string.push_back(s[i]);
18+
}
19+
}
20+
else {
21+
result_string.push_back(s[i]);
22+
}
23+
}
24+
// now "stack_for_characters.size()" is the number of wrong "left" parentheses
25+
26+
// remove wrong left parentheses
27+
while (!stack_for_index.empty())
28+
{
29+
result_string.erase(stack_for_index.top(), 1);
30+
stack_for_index.pop();
31+
}
32+
33+
return result_string;
34+
}
35+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
203203
| 394 | [Decode String](https://leetcode.com/problems/decode-string/) | [C++](./C++/decode-string.cpp) | _O(n)_ | _O(1)_ | Medium | Stack | |
204204
| 921 | [Minimum Add to Make Parentheses Valid](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/) | [C++](./C++/minimum-add-to-make-parentheses-valid.cpp) | _O(n)_ | _O(1)_ | Medium | Stack | |
205205
| 32 | [Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/) | [Python](.Python/longest-valid-parentheses.py) | _O(n)_ | _O(n)_ | Hard | Stack | |
206+
| 1249 | [Minimum Remove to Make Valid Parentheses](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/) | [C++](./C++/minimum-remove-to-make-valid-parentheses.cpp) | _O(n)_ | _O(n)_ | Medium | Stack | |
206207
<br/>
207208
<div align="right">
208209
<b><a href="#algorithms">⬆️ Back to Top</a></b>

0 commit comments

Comments
 (0)