LeetCode 1750, Medium. Topics: Two Pointers, String. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 30 parametrized pytest cases, and a playground notebook:
Problem
<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>
Examples
Explanation: You can’t remove any characters, so the string stays as is.
Explanation: 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 = "".
Explanation: 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”.
Constraints
1 <= s.length <= 10^5
s consists only of characters 'a', 'b', and 'c'.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026