-
-
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 - 60 ms (89.35%), Memory - 17.2 MB (…
…84.27%)
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
1850-minimum-length-of-string-after-deleting-similar-ends/README.md
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,49 @@ | ||
<p>Given a string <code>s</code> consisting only of characters <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>. You are asked to apply the following algorithm on the string any number of times:</p> | ||
|
||
<ol> | ||
<li>Pick a <strong>non-empty</strong> prefix from the string <code>s</code> where all the characters in the prefix are equal.</li> | ||
<li>Pick a <strong>non-empty</strong> suffix from the string <code>s</code> where all the characters in this suffix are equal.</li> | ||
<li>The prefix and the suffix should not intersect at any index.</li> | ||
<li>The characters from the prefix and suffix must be the same.</li> | ||
<li>Delete both the prefix and the suffix.</li> | ||
</ol> | ||
|
||
<p>Return <em>the <strong>minimum length</strong> of </em><code>s</code> <em>after performing the above operation any number of times (possibly zero times)</em>.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> s = "ca" | ||
<strong>Output:</strong> 2 | ||
<strong>Explanation: </strong>You can't remove any characters, so the string stays as is. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> s = "cabaabac" | ||
<strong>Output:</strong> 0 | ||
<strong>Explanation:</strong> An optimal sequence of operations is: | ||
- Take prefix = "c" and suffix = "c" and remove them, s = "abaaba". | ||
- Take prefix = "a" and suffix = "a" and remove them, s = "baab". | ||
- Take prefix = "b" and suffix = "b" and remove them, s = "aa". | ||
- Take prefix = "a" and suffix = "a" and remove them, s = "".</pre> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> s = "aabccabba" | ||
<strong>Output:</strong> 3 | ||
<strong>Explanation:</strong> An optimal sequence of operations is: | ||
- Take prefix = "aa" and suffix = "a" and remove them, s = "bccabb". | ||
- Take prefix = "b" and suffix = "bb" and remove them, s = "cca". | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li> | ||
<li><code>s</code> only consists of characters <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>.</li> | ||
</ul> |
17 changes: 17 additions & 0 deletions
17
1850-minimum-length-of-string-after-deleting-similar-ends/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,17 @@ | ||
# Approach 1: Two Pointers | ||
|
||
class Solution: | ||
def minimumLength(self, s: str) -> int: | ||
begin, end = 0, len(s) - 1 | ||
|
||
while begin < end and s[begin] == s[end]: | ||
c = s[begin] | ||
|
||
while begin <= end and s[begin] == c: | ||
begin += 1 | ||
|
||
while end > begin and s[end] == c: | ||
end -= 1 | ||
|
||
return end - begin + 1 | ||
|