---
# System prepended metadata

title: 2730. Find the Longest Semi-Repetitive Substring
tags: [Leetcode, Sliding Window, Medium]

---

Link: https://leetcode.com/problems/find-the-longest-semi-repetitive-substring/description/
## 思路
sliding window 保证window里只有最多一个consecutive pair of the same digits
如果已经有一个又遇到另一个左指针挪到上一个consecutive pair第二个element的位置
## Code
```python=
class Solution:
    def longestSemiRepetitiveSubstring(self, s: str) -> int:
        ans, l = 0, 0
        pairExist = False
        pairPos = -1
        for r in range(len(s)-1):
            if s[r]==s[r+1]:
                if not pairExist:
                    pairExist = True
                else:
                    ans = max(ans, r-l+1)
                    l = pairPos+1
                pairPos = r
        ans = max(ans, len(s)-l)
        return ans
```