Maximize Active Section with Trade I

You are given a binary string s of length n, where:

  • '1' represents an active section.
  • '0' represents an inactive section.

You can perform at most one trade to maximize the number of active sections in s. In a trade, you:

  • Convert a contiguous block of '1's that is surrounded by '0's to all '0's.
  • Afterward, convert a contiguous block of '0's that is surrounded by '1's to all '1's.

Return the maximum number of active sections in s after making the optimal trade.

Note: Treat s as if it is augmented with a '1' at both ends, forming t = '1' + s + '1'. The augmented '1's do not contribute to the final count.

Example 1
Inputs = "01"
Output1
Because there is no block of '1's surrounded by '0's, no valid trade is possible, so the maximum number of active sections is 1.
Example 2
Inputs = "0100"
Output4
The optimal trade converts the middle surrounded '1' to '0' and then converts the resulting surrounded block of zeros to ones, making the final string without augmentation "1111".

Constraints

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

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate