diff --git a/C++/Network-delay-time.cpp.txt b/C++/Network-delay-time.cpp similarity index 100% rename from C++/Network-delay-time.cpp.txt rename to C++/Network-delay-time.cpp diff --git a/C++/minimum-remove-to-make-valid-parentheses.cpp b/C++/minimum-remove-to-make-valid-parentheses.cpp new file mode 100644 index 00000000..370a3345 --- /dev/null +++ b/C++/minimum-remove-to-make-valid-parentheses.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + string minRemoveToMakeValid(string s) { + stack 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; + } +}; \ No newline at end of file diff --git a/README.md b/README.md index 2b5ce843..538778af 100644 --- a/README.md +++ b/README.md @@ -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 | |
⬆️ Back to Top