> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum Length of String After Deleting

> Tested Python solution for LeetCode 1750 with 30 pytest cases. Generate a practice environment with lcpy.

LeetCode 1750, [Medium](/catalog/medium). Topics: [Two Pointers](/catalog/topics/two-pointers), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/description/).

Generate this problem as a practice environment: tested reference solution, 30 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1750   # by problem number
lcpy gen -s minimum_length_of_string_after_deleting_similar_ends   # by problem name
```

## 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

```
Input: s = "ca"
Output: 2
```

**Explanation:** You can't remove any characters, so the string stays as is.

```
Input: s = "cabaabac"
Output: 0
```

**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 = "".

```
Input: s = "aabccabba"
Output: 3
```

**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](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_length_of_string_after_deleting_similar_ends/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_length_of_string_after_deleting_similar_ends/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def minimum_length(self, s: str) -> int:
        left, right = 0, len(s) - 1
        while left < right and s[left] == s[right]:
            ch = s[left]
            while left <= right and s[left] == ch:
                left += 1
            while left <= right and s[right] == ch:
                right -= 1
        return right - left + 1
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.