-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission Runtime - 136 ms (60.63%), Memory - 18.9 MB …
…(14.92%)
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<p>You are given a string <code>s</code>.</p> | ||
|
||
<p>You can perform the following process on <code>s</code> <strong>any</strong> number of times:</p> | ||
|
||
<ul> | ||
<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> | ||
<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> | ||
<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> | ||
</ul> | ||
|
||
<p>Return the <strong>minimum</strong> length of the final string <code>s</code> that you can achieve.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>Input:</strong> <span class="example-io">s = "abaacbcbb"</span></p> | ||
|
||
<p><strong>Output:</strong> <span class="example-io">5</span></p> | ||
|
||
<p><strong>Explanation:</strong><br /> | ||
We do the following operations:</p> | ||
|
||
<ul> | ||
<li>Choose index 2, then remove the characters at indices 0 and 3. The resulting string is <code>s = "bacbcbb"</code>.</li> | ||
<li>Choose index 3, then remove the characters at indices 0 and 5. The resulting string is <code>s = "acbcb"</code>.</li> | ||
</ul> | ||
</div> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>Input:</strong> <span class="example-io">s = "aa"</span></p> | ||
|
||
<p><strong>Output:</strong> <span class="example-io">2</span></p> | ||
|
||
<p><strong>Explanation:</strong><br /> | ||
We cannot perform any operations, so we return the length of the original string.</p> | ||
</div> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= s.length <= 2 * 10<sup>5</sup></code></li> | ||
<li><code>s</code> consists only of lowercase English letters.</li> | ||
</ul> |
25 changes: 25 additions & 0 deletions
25
3455-minimum-length-of-string-after-operations/solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Approach 1: Using Hash Map | ||
|
||
# n = len(s), k = len(character set) | ||
# Time: O(n) | ||
# Space: O(k) = O(1) | ||
|
||
from collections import Counter | ||
|
||
class Solution: | ||
def minimumLength(self, s: str) -> int: | ||
char_freq = Counter(s) | ||
|
||
delete_count = 0 | ||
|
||
for freq in char_freq.values(): | ||
if freq % 2 == 1: | ||
# If frequency is odd, delete all except one | ||
delete_count += freq - 1 | ||
else: | ||
# If frequency is even, delete all except two | ||
delete_count += freq - 2 | ||
|
||
# Return the minimum length after deletions | ||
return len(s) - delete_count | ||
|