1850. Minimum Length of String After Deleting Similar Ends
My accepted Python solution to LeetCode problem 1850, Minimum Length of String After Deleting Similar Ends, running in 27ms.
- Difficulty: Medium
- Python
- Runtime 27ms
- Memory 18.4MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 27ms, memory 18.4MB, accepted 2025-12-30.
class Solution:
def minimumLength(self, s: str) -> int:
left, right = 0, len(s) - 1
while left < right and s[left] == s[right]:
char = s[left]
# Move left pointer past all matching characters
while left <= right and s[left] == char:
left += 1
# Move right pointer past all matching characters
while left <= right and s[right] == char:
right -= 1
return right - left + 1