Interview Questions/Coding/Balanced Feed Split Counter

Balanced Feed Split Counter

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Balanced Feed Split Counter

A binary feed is scanned from left to right. A segment is balanced if it contains the same number of 0 characters and 1 characters. During the scan, whenever the current open segment becomes balanced, it is cut immediately and a new segment begins after it. Determine how many balanced segments are created by this process. The scan must preserve the original order of the feed. Input Format: You are given a binary string s. Output Format: Return the maximum number of balanced segments created by the required left-to-right scan.

Examples

Example 1
Input:
s = "01001110"
Output:
3

Explanation: The feed splits as ["01", "0011", "10"], and every chunk has equal zeroes and ones. Number of chunks = 3, so the output is 3.

Approach hint

Track the difference between ones and zeroes.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp