↓Skip to main content
  1. LeetCode/

LeetCode 2730: Find the Longest Semi-Repetitive Substring

·2 mins· ·
LeetCode Medium Sliding-Window String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-11

  • Submissions: Accepted (first try)

Approach
#

A window is valid ⇔ it contains at most one adjacent equal pair (s[i] == s[i + 1]).

  • When s[right] is added and s[right] == s[right - 1], the pair count goes up by 1.
  • With more than one pair, shrink the left. When s[left] leaves and s[left] == s[left + 1], that pair leaves too, so the count goes down by 1.

My Solution
#

class Solution:
    def longestSemiRepetitiveSubstring(self, s: str) -> int:
        left = pairs = best = 0
        for right in range(len(s)):
            if right > 0 and s[right] == s[right - 1]:
                pairs += 1
            while pairs > 1:
                if s[left] == s[left + 1]:
                    pairs -= 1                     # this pair leaves the window
                left += 1
            best = max(best, right - left + 1)
        return best

With pairs > 1 the window holds at least two pairs, so left + 1 <= right and s[left + 1] stays in range.

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • When the left leaves, check the pair s[left], s[left + 1], not s[left - 1].

Interview Explanation
#

The window may contain at most one adjacent equal pair. Adding a character equal to the previous one adds a pair; with more than one, I shrink the left, and when the leaving character equals the next one, that pair is gone. Tracking the longest length gives O(n).

Related

LeetCode 1208: Get Equal Substrings Within Budget
·1 min
LeetCode Medium Sliding-Window String Binary-Search Prefix-Sum
LeetCode 1208 note: each position costs |s[i] - t[i]|, so the problem becomes the longest subarray with sum <= maxCost
LeetCode 2024: Maximize the Confusion of an Exam
·2 mins
LeetCode Medium Sliding-Window String Binary-Search Prefix-Sum
LeetCode 2024 note: a window is valid when the rarer of T and F appears at most k times (flip all of them); shrink the left when it is not
LeetCode 2516: Take K of Each Character From Left and Right
·2 mins
LeetCode Medium Sliding-Window Hash-Table String
LeetCode 2516 note: taking from both ends leaves a middle block in which each letter may appear at most total − k times; find the longest such window
LeetCode 1016: Binary String With Substrings Representing 1 To N
·3 mins
LeetCode Medium Sliding-Window String Bit-Manipulation Hash-Table
LeetCode 1016 note: s has at most 1000 characters, so there are few substrings of any fixed length and a large n must be False; checking from n downward fails fast, or extend up to 30 bits from each start to collect every value <= n
LeetCode 1888: Minimum Number of Flips to Make the Binary String Alternating
·2 mins
LeetCode Medium Sliding-Window String Dynamic-Programming
LeetCode 1888 note: rotations become length-n windows over s + s; the window’s mismatches against 0101… are diff, and against 1010… they are n - diff
LeetCode 3694: Distinct Points Reachable After Substring Removal
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table String
LeetCode 3694 note: the endpoint is the total displacement minus the removed segment’s displacement; a length-k window tracks that segment and each endpoint goes into a set