Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
35 changes: 35 additions & 0 deletions C++/minimum-remove-to-make-valid-parentheses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public:
string minRemoveToMakeValid(string s) {
stack<int> stack_for_index; //stack to save index of '('
string result_string = "";

// count wrong parentheses with stack & make string
for (int i = 0; i < s.size(); i++)
{
if (s[i] == '(') {
stack_for_index.push(result_string.size());
result_string.push_back(s[i]);
}
else if (s[i] == ')') {
if (!stack_for_index.empty()) {
stack_for_index.pop();
result_string.push_back(s[i]);
}
}
else {
result_string.push_back(s[i]);
}
}
// now "stack_for_characters.size()" is the number of wrong "left" parentheses

// remove wrong left parentheses
while (!stack_for_index.empty())
{
result_string.erase(stack_for_index.top(), 1);
stack_for_index.pop();
}

return result_string;
}
};
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| 394 | [Decode String](https://leetcode.com/problems/decode-string/) | [C++](./C++/decode-string.cpp) | _O(n)_ | _O(1)_ | Medium | Stack | |
| 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 | |
| 32 | [Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/) | [Python](.Python/longest-valid-parentheses.py) | _O(n)_ | _O(n)_ | Hard | Stack | |
| 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 | |
<br/>
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
Expand Down