Watch 10 video solutions for Find the Longest Semi-Repetitive Substring, a medium level problem involving String, Sliding Window. This walkthrough by code Explainer has 894 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a digit string s that consists of digits from 0 to 9.
A string is called semi-repetitive if there is at most one adjacent pair of the same digit. For example, "0010", "002020", "0123", "2002", and "54944" are semi-repetitive while the following are not: "00101022" (adjacent same digit pairs are 00 and 22), and "1101234883" (adjacent same digit pairs are 11 and 88).
Return the length of the longest semi-repetitive substring of s.
Example 1:
Input: s = "52233"
Output: 4
Explanation:
The longest semi-repetitive substring is "5223". Picking the whole string "52233" has two adjacent same digit pairs 22 and 33, but at most one is allowed.
Example 2:
Input: s = "5494"
Output: 4
Explanation:
s is a semi-repetitive string.
Example 3:
Input: s = "1111111"
Output: 2
Explanation:
The longest semi-repetitive substring is "11". Picking the substring "111" has two adjacent same digit pairs, but at most one is allowed.
Constraints:
1 <= s.length <= 50'0' <= s[i] <= '9'Problem Overview: Given a numeric string s, return the length of the longest substring that is semi-repetitive. A substring is semi-repetitive if it contains at most one pair of equal adjacent characters (for example "11"). The goal is to scan the string and keep the longest window where this condition holds.
Approach 1: Sliding Window (O(n) time, O(1) space)
This problem fits naturally into a sliding window pattern. Maintain two pointers left and right that represent the current substring window. While expanding the window to the right, count how many adjacent equal pairs appear (i.e., when s[i] == s[i-1]). If the count exceeds one, move the left pointer forward until the window again contains at most one such pair. Each character enters and leaves the window at most once, so the scan runs in O(n) time with constant extra memory. This approach is optimal and very common in substring problems involving constraints.
Approach 2: Dynamic Programming Tracking Adjacent Pairs (O(n) time, O(1) space)
A dynamic programming view keeps track of the most recent index where an adjacent duplicate occurred. While iterating through the string, store the index of the previous duplicate pair and the one before it. If a new pair appears, update the window start to the position after the earlier pair so that the substring still contains at most one duplicate pair. This effectively maintains the longest valid substring ending at each position. The algorithm still performs a single pass through the string, giving O(n) time and constant space. Some implementations describe this as DP because the current state depends on previously tracked pair positions.
Recommended for interviews: The sliding window solution is the approach most interviewers expect. It demonstrates strong control over substring constraints and two-pointer techniques. A brute-force enumeration of all substrings would show understanding but runs in O(n^2) or worse. The optimal sliding window solution proves you can maintain constraints efficiently while scanning the string once.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sliding Window | O(n) | O(1) | Best general solution for substring constraints with limited violations |
| Dynamic Programming (Track Duplicate Pair Positions) | O(n) | O(1) | Useful when modeling the problem as state transitions while scanning the string |