Ctrl K

One-Double Digit Stretch

Medium
You are given a string digits made of digits. Call two equal digits standing next to each other a double. For example, "7337" has one double (33), "1220055" has three (22, 00, 55), and "5555" has three as well, since each neighbouring pair counts separately. Return the length of the longest contiguous piece of digits that contains at most one double.
Example 1
digits="3445667" → 5

The whole string has two doubles, 44 and 66. The piece "34456" contains only 44 and has length 5; so does "45667".

Example 2
digits="9080" → 4

There are no equal neighbouring digits at all, so the whole string works.

Example 3
digits="000" → 2

"000" contains two pairs of neighbouring zeros while "00" has one, so the answer is 2.

Constraints
  • 1 ≤ len(digits) ≤ 10⁵
  • digits consists of digits 0 to 9
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Repeat-Free Stretch MediumTwo-Letter Stretch MediumRecurring Gene Windows MediumWindow with Few Kinds of Letters MediumEvery Letter at Least k Times Medium
loading Python…
Input
digits="3445667"
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 200 tests on the server, hidden ones included.