---
# System prepended metadata

title: 'Leetcode [No. 1750] Minimum Length of String After Deleting Similar Ends (MEDIUM) 解題心得'
tags: [leetcode]

---

# Leetcode [No. 1750] Minimum Length of String After Deleting Similar Ends (MEDIUM) 解題心得

+ Solved on 2024/03/05 Daily Challenge

## 題目

https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/description/

## 思路
+ 這題一開始我的bug是de不太出來的，後來看了solution100%才知道原來是我在line 9 跟line13的`front <= end` 寫成`front < end`，所以沒辦法處理這個撞在一起的case

```c++=
class Solution {
public:
    int minimumLength(string s) {
        int front = 0, end = s.size()-1;
        while(front < end && s[front] == s[end])
        {
            front++;
            end--;
            while(front <= end && s[front-1] == s[front])
            {
                front++;
            }
            while(front <= end && s[end+1] == s[end])                
            {
                end--;
            }

        }
        // cout << front << "\t" << end << endl;
        return end - front + 1;
    }
};
```

### 解法分析
+ time complexity: O(n)


### 執行結果

![image](https://hackmd.io/_uploads/ByCyTEV6T.png)

