Skip to content

Commit 980ad1f

Browse files
committed
Sync LeetCode submission Runtime - 48 ms (37.79%), Memory - 19.7 MB (13.90%)
1 parent 1fdf93c commit 980ad1f

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p>
2+
3+
<p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses string</em> is valid and return <strong>any</strong> valid string.</p>
4+
5+
<p>Formally, a <em>parentheses string</em> is valid if and only if:</p>
6+
7+
<ul>
8+
<li>It is the empty string, contains only lowercase characters, or</li>
9+
<li>It can be written as <code>AB</code> (<code>A</code> concatenated with <code>B</code>), where <code>A</code> and <code>B</code> are valid strings, or</li>
10+
<li>It can be written as <code>(A)</code>, where <code>A</code> is a valid string.</li>
11+
</ul>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> s = &quot;lee(t(c)o)de)&quot;
18+
<strong>Output:</strong> &quot;lee(t(c)o)de&quot;
19+
<strong>Explanation:</strong> &quot;lee(t(co)de)&quot; , &quot;lee(t(c)ode)&quot; would also be accepted.
20+
</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> s = &quot;a)b(c)d&quot;
26+
<strong>Output:</strong> &quot;ab(c)d&quot;
27+
</pre>
28+
29+
<p><strong class="example">Example 3:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong> s = &quot;))((&quot;
33+
<strong>Output:</strong> &quot;&quot;
34+
<strong>Explanation:</strong> An empty string is also valid.
35+
</pre>
36+
37+
<p>&nbsp;</p>
38+
<p><strong>Constraints:</strong></p>
39+
40+
<ul>
41+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
42+
<li><code>s[i]</code> is either&nbsp;<code>&#39;(&#39;</code> , <code>&#39;)&#39;</code>, or lowercase English letter.</li>
43+
</ul>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Approach 1: Using Stack and String Builder
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def minRemoveToMakeValid(self, s: str) -> str:
8+
indices_to_remove = set()
9+
stack = [] # tracks the index
10+
11+
for i, c in enumerate(s):
12+
if c not in "()":
13+
continue
14+
elif c == '(':
15+
stack.append(i)
16+
elif not stack:
17+
indices_to_remove.add(i)
18+
elif c == ')':
19+
stack.pop()
20+
21+
indices_to_remove = indices_to_remove.union(set(stack))
22+
23+
string_builder = []
24+
for i, c in enumerate(s):
25+
if i not in indices_to_remove:
26+
string_builder.append(c)
27+
28+
return ''.join(string_builder)
29+

0 commit comments

Comments
 (0)