Skip to content

Commit f4d9344

Browse files
committed
Sync LeetCode submission Runtime - 60 ms (89.35%), Memory - 17.2 MB (84.27%)
1 parent b7d0174 commit f4d9344

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<p>Given a string <code>s</code> consisting only of characters <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>. You are asked to apply the following algorithm on the string any number of times:</p>
2+
3+
<ol>
4+
<li>Pick a <strong>non-empty</strong> prefix from the string <code>s</code> where all the characters in the prefix are equal.</li>
5+
<li>Pick a <strong>non-empty</strong> suffix from the string <code>s</code> where all the characters in this suffix are equal.</li>
6+
<li>The prefix and the suffix should not intersect at any index.</li>
7+
<li>The characters from the prefix and suffix must be the same.</li>
8+
<li>Delete both the prefix and the suffix.</li>
9+
</ol>
10+
11+
<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>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> s = &quot;ca&quot;
18+
<strong>Output:</strong> 2
19+
<strong>Explanation: </strong>You can&#39;t remove any characters, so the string stays as is.
20+
</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> s = &quot;cabaabac&quot;
26+
<strong>Output:</strong> 0
27+
<strong>Explanation:</strong> An optimal sequence of operations is:
28+
- Take prefix = &quot;c&quot; and suffix = &quot;c&quot; and remove them, s = &quot;abaaba&quot;.
29+
- Take prefix = &quot;a&quot; and suffix = &quot;a&quot; and remove them, s = &quot;baab&quot;.
30+
- Take prefix = &quot;b&quot; and suffix = &quot;b&quot; and remove them, s = &quot;aa&quot;.
31+
- Take prefix = &quot;a&quot; and suffix = &quot;a&quot; and remove them, s = &quot;&quot;.</pre>
32+
33+
<p><strong class="example">Example 3:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> s = &quot;aabccabba&quot;
37+
<strong>Output:</strong> 3
38+
<strong>Explanation:</strong> An optimal sequence of operations is:
39+
- Take prefix = &quot;aa&quot; and suffix = &quot;a&quot; and remove them, s = &quot;bccabb&quot;.
40+
- Take prefix = &quot;b&quot; and suffix = &quot;bb&quot; and remove them, s = &quot;cca&quot;.
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
48+
<li><code>s</code> only consists of characters <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</li>
49+
</ul>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Approach 1: Two Pointers
2+
3+
class Solution:
4+
def minimumLength(self, s: str) -> int:
5+
begin, end = 0, len(s) - 1
6+
7+
while begin < end and s[begin] == s[end]:
8+
c = s[begin]
9+
10+
while begin <= end and s[begin] == c:
11+
begin += 1
12+
13+
while end > begin and s[end] == c:
14+
end -= 1
15+
16+
return end - begin + 1
17+

0 commit comments

Comments
 (0)