All problems
0493MediumStringEnumeration

Sign Segment Repair Pass

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3499Maximize Active Section with Trade I

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A shop sign is a strip of n segments described by the string strip, where '1' is a segment that is currently lit and '0' is one that is dark.

A technician gets one repair pass over the strip, and the pass has two steps taken in this order:

  1. Pick a stretch of consecutive segments that are all lit as the strip stands, and switch every segment in that stretch off.
  2. Pick a stretch of consecutive segments that are all dark in the strip step 1 left behind, and switch every segment in that stretch on.

Each stretch must be consecutive and must be uniform in the state its step calls for. Either stretch is allowed to be empty, and the two steps cannot be reordered or repeated.

Return the largest number of lit segments the sign can show once the pass is over.

Examples

Example 1

Input
strip = "1101000110"
Output
9

Switching off the lit segment at index 3 welds the dark segment at index 2 to the dark stretch at indices 4 through 6, and switching that whole stretch of five on leaves nine lit.

Example 2

Input
strip = "000101000"
Output
6

Switching off the lit segment at index 3 joins the dark stretch at indices 0 through 2 to the dark segment at index 4, and switching those four on leaves six lit.

Example 3

Input
strip = "10011101"
Output
8

Switching off nothing and then switching on the dark stretch at indices 1 and 2 leaves seven lit.

Constraints

  • 1 <= n == strip.length <= 10^5
  • strip[i] is either '0' or '1'.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def best_lit_after_pass(strip: str) -> int:
Java
public int bestLitAfterPass(String strip)
September 7
Apply