-
Notifications
You must be signed in to change notification settings - Fork 0
/
1249.cpp
53 lines (47 loc) · 1.25 KB
/
1249.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
#include <stdio.h>
#include <string>
#include <stack>
#include <iostream>
using namespace std;
/*
Runtime: 48 ms, faster than 31.72% of C++ online submissions for Minimum Remove to Make Valid Parentheses.
Memory Usage: 11.9 MB, less than 49.60% of C++ online submissions for Minimum Remove to Make Valid Parentheses.
*/
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;
}
};
int main(void) {
Solution sol = Solution();
string input = "lee(t(c)o)de)";
cout << sol.minRemoveToMakeValid(input) << "\n";
return 0;
}