Tap here to switch tabs
Problem
Submissions

Problem: Longest Balanced Substring After One Swap

Try to solve the Longest Balanced Substring After One Swap problem.

Statement

You are given a binary string s consisting only of the characters '0' and '1'.

You may choose to swap any two characters in s at most once.

A substring of s is called balanced if the count of '0' characters is equal to the count of '1' characters within that substring.

Return the maximum possible length of a balanced substring that can be formed after at most one swap.

Constraints:

  • 1≤1 \leq s.length ≤105\leq 10^5

  • s consists only of the characters '0' and '1'.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Longest Balanced Substring After One Swap

Try to solve the Longest Balanced Substring After One Swap problem.

Statement

You are given a binary string s consisting only of the characters '0' and '1'.

You may choose to swap any two characters in s at most once.

A substring of s is called balanced if the count of '0' characters is equal to the count of '1' characters within that substring.

Return the maximum possible length of a balanced substring that can be formed after at most one swap.

Constraints:

  • 1≤1 \leq s.length ≤105\leq 10^5

  • s consists only of the characters '0' and '1'.