Skip to content

Commit 3d97069

Browse files
committed
Sync LeetCode submission Runtime - 136 ms (60.63%), Memory - 18.9 MB (14.92%)
1 parent c7d6865 commit 3d97069

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<p>You are given a string <code>s</code>.</p>
2+
3+
<p>You can perform the following process on <code>s</code> <strong>any</strong> number of times:</p>
4+
5+
<ul>
6+
<li>Choose an index <code>i</code> in the string such that there is <strong>at least</strong> one character to the left of index <code>i</code> that is equal to <code>s[i]</code>, and <strong>at least</strong> one character to the right that is also equal to <code>s[i]</code>.</li>
7+
<li>Delete the <strong>closest</strong> character to the <strong>left</strong> of index <code>i</code> that is equal to <code>s[i]</code>.</li>
8+
<li>Delete the <strong>closest</strong> character to the <strong>right</strong> of index <code>i</code> that is equal to <code>s[i]</code>.</li>
9+
</ul>
10+
11+
<p>Return the <strong>minimum</strong> length of the final string <code>s</code> that you can achieve.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<div class="example-block">
17+
<p><strong>Input:</strong> <span class="example-io">s = &quot;abaacbcbb&quot;</span></p>
18+
19+
<p><strong>Output:</strong> <span class="example-io">5</span></p>
20+
21+
<p><strong>Explanation:</strong><br />
22+
We do the following operations:</p>
23+
24+
<ul>
25+
<li>Choose index 2, then remove the characters at indices 0 and 3. The resulting string is <code>s = &quot;bacbcbb&quot;</code>.</li>
26+
<li>Choose index 3, then remove the characters at indices 0 and 5. The resulting string is <code>s = &quot;acbcb&quot;</code>.</li>
27+
</ul>
28+
</div>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<div class="example-block">
33+
<p><strong>Input:</strong> <span class="example-io">s = &quot;aa&quot;</span></p>
34+
35+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
36+
37+
<p><strong>Explanation:</strong><br />
38+
We cannot perform any operations, so we return the length of the original string.</p>
39+
</div>
40+
41+
<p>&nbsp;</p>
42+
<p><strong>Constraints:</strong></p>
43+
44+
<ul>
45+
<li><code>1 &lt;= s.length &lt;= 2 * 10<sup>5</sup></code></li>
46+
<li><code>s</code> consists only of lowercase English letters.</li>
47+
</ul>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Approach 1: Using Hash Map
2+
3+
# n = len(s), k = len(character set)
4+
# Time: O(n)
5+
# Space: O(k) = O(1)
6+
7+
from collections import Counter
8+
9+
class Solution:
10+
def minimumLength(self, s: str) -> int:
11+
char_freq = Counter(s)
12+
13+
delete_count = 0
14+
15+
for freq in char_freq.values():
16+
if freq % 2 == 1:
17+
# If frequency is odd, delete all except one
18+
delete_count += freq - 1
19+
else:
20+
# If frequency is even, delete all except two
21+
delete_count += freq - 2
22+
23+
# Return the minimum length after deletions
24+
return len(s) - delete_count
25+

0 commit comments

Comments
 (0)