Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit decf43b

Browse filesBrowse files
authored
Create 19 removeBracketsValidPalindrome.cpp
1 parent edfcc29 commit decf43b
Copy full SHA for decf43b

File tree

Expand file treeCollapse file tree

1 file changed

+33
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+33
-0
lines changed
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
string minRemoveToMakeValid(string s) {
4+
5+
stack<int> stack;
6+
int n = s.size();
7+
8+
for (int i = 0; i < n; i++) {
9+
if (s[i] == '(')
10+
stack.push(i);
11+
12+
else if (s[i] == ')') {
13+
14+
if (!stack.empty()) {
15+
stack.pop();
16+
}
17+
// Deleting extra ) on the fly
18+
else {
19+
s.erase(i);
20+
i--;
21+
}
22+
}
23+
}
24+
25+
// Deleting extra ( here
26+
while (!stack.empty()) {
27+
s.erase(stack.top());
28+
stack.pop();
29+
}
30+
return s;
31+
32+
}
33+
};

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.