Skip to content

Practice Find the Longest Semi-Repetitive Substring

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2730
LeetCode Problem

Find the Longest Semi-Repetitive Substring

Problem Statement

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.

Source: Find the Longest Semi-Repetitive Substring on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "52233"
Output
4

# Example 2

Input
s = "5494"
Output
4

# Example 3

Input
s = "1111111"
Output
2

Constraints

1 <= s.length <= 50
'0' <= s[i] <= '9'

Related LeetCode Problems

Practice more problems similar to Find the Longest Semi-Repetitive Substring.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.