-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1249.cpp
More file actions
34 lines (30 loc) · 872 Bytes
/
1249.cpp
File metadata and controls
34 lines (30 loc) · 872 Bytes
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
// Problem : 1249. Minimum Remove to Make Valid Parentheses
// Link : https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/
#include <iostream>
#include <string>
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string minRemoveToMakeValid(string s) {
stack<int> bracket_pos;
for(int i = 0; i < s.size(); i++){
if(s[i] == '(')
bracket_pos.push(i);
else if(s[i] == ')' && !bracket_pos.empty())
bracket_pos.pop();
else if(s[i] == ')')
s.erase(s.begin() + i--);
}
while(!bracket_pos.empty()){
s.erase(s.begin() + bracket_pos.top());
bracket_pos.pop();
}
return s;
}
};
int main() {
Solution ob;
cout << ob.minRemoveToMakeValid("lee(t(c)o)de)");
return 0;
}