Longest Balanced Binary Substring After at Most One Swap

Quick Overview

Find the longest contiguous binary substring with equal zero and one counts after at most one swap anywhere in the string, including across its boundary.

Longest Balanced Binary Substring After at Most One Swap

Company: Tekion

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Longest Balanced Binary Substring After at Most One Swap You are given a binary string `s`. You may swap the characters at any two positions at most once, then choose one contiguous substring of the resulting string. A substring is balanced when it contains equally many `'0'` and `'1'` characters, in any order. Return the greatest possible length of a balanced substring. The swapped positions need not be adjacent, and either position may lie outside the substring you eventually select. You may also perform no swap. Return `0` when only the empty substring is balanced. ### Function `int longestBalanced(string s)` ### Example 1 **Input:** `s = "1100000001"` **Output:** `6` **Explanation:** Swap the characters at zero-based positions `3` and `9` to obtain `"1101000000"`. Its first six characters, `"110100"`, contain three zeros and three ones. No balanced substring can be longer because the whole string has only three ones, and a swap preserves that total. ### Example 2 **Input:** `s = "0011111100"` **Output:** `6` **Explanation:** Swap positions `2` and `8` to obtain `"0001111110"`, whose first six characters form the balanced substring `"000111"`. Each of the three length-eight windows originally contains six ones and two zeros, so one swap cannot balance it. The full length-ten string also cannot become balanced because its total counts are unequal. ### Constraints - `1 <= s.length <= 100000` - Every character of `s` is either `'0'` or `'1'`. ### Clarifications - The selected characters must be contiguous; this is not a subsequence problem. - Balanced refers only to the counts, not to a required arrangement of zeros and ones. - A swap exchanges existing characters and cannot change the string's total counts. ### What a Strong Answer Covers - Considering swaps that bring a character into the selected substring from outside it. - Handling no-swap choices and strings containing only one character value. - Returning the maximum feasible contiguous length for the full input size.

Overview: Find the longest contiguous binary substring with equal zero and one counts after at most one swap anywhere in the string, including across its boundary.

Tekion logo
Tekion
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Longest Balanced Binary Substring After at Most One Swap

You are given a binary string s. You may swap the characters at any two positions at most once, then choose one contiguous substring of the resulting string.

A substring is balanced when it contains equally many '0' and '1' characters, in any order. Return the greatest possible length of a balanced substring.

The swapped positions need not be adjacent, and either position may lie outside the substring you eventually select. You may also perform no swap. Return 0 when only the empty substring is balanced.

Function

int longestBalanced(string s)

Example 1

Input: s = "1100000001"

Output: 6

Explanation: Swap the characters at zero-based positions 3 and 9 to obtain "1101000000". Its first six characters, "110100", contain three zeros and three ones. No balanced substring can be longer because the whole string has only three ones, and a swap preserves that total.

Example 2

Input: s = "0011111100"

Output: 6

Explanation: Swap positions 2 and 8 to obtain "0001111110", whose first six characters form the balanced substring "000111". Each of the three length-eight windows originally contains six ones and two zeros, so one swap cannot balance it. The full length-ten string also cannot become balanced because its total counts are unequal.

Constraints

  • 1 <= s.length <= 100000
  • Every character of s is either '0' or '1' .

Clarifications

  • The selected characters must be contiguous; this is not a subsequence problem.
  • Balanced refers only to the counts, not to a required arrangement of zeros and ones.
  • A swap exchanges existing characters and cannot change the string's total counts.

What a Strong Answer Covers Guidance

  • Considering swaps that bring a character into the selected substring from outside it.
  • Handling no-swap choices and strings containing only one character value.
  • Returning the maximum feasible contiguous length for the full input size.