Count Equal-Size Adjacent Binary Runs

Quick Overview

Count Equal-Size Adjacent Binary Runs. Return the number of nonempty substrings of s containing the same positive number of zeros and ones, with all zeros consecutive and all ones consecutive within that substring.

Count Equal-Size Adjacent Binary Runs

Company: Deshaw

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Return the number of nonempty substrings of s containing the same positive number of zeros and ones, with all zeros consecutive and all ones consecutive within that substring. Count each start/end occurrence separately. ```cpp class Solution { public: int countBinarySubstrings(string s); }; ``` ### Examples **Example 1:** ```text s = "00011" Output: 2 Explanation: The valid ranges have text "01" and "0011". ``` **Example 2:** ```text s = "110100" Output: 3 Explanation: Run lengths 2,1,1,2 contribute 1+1+1. ``` ### Constraints - Input length: 1..100000 characters. The alphabet contains only zero and one. ### Clarifying Questions Require exactly two adjacent equal-size character blocks and retain repeated occurrences. ```hint Read the boundary condition How many balanced substrings can straddle one boundary between runs of lengths a and b? ``` ### Contract considerations Require exactly two adjacent equal-size character blocks and retain repeated occurrences.

Overview: Count Equal-Size Adjacent Binary Runs. Return the number of nonempty substrings of s containing the same positive number of zeros and ones, with all zeros consecutive and all ones consecutive within that substring.

Deshaw logo
Deshaw
Sep 9, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return the number of nonempty substrings of s containing the same positive number of zeros and ones, with all zeros consecutive and all ones consecutive within that substring. Count each start/end occurrence separately.

class Solution {
public:
    int countBinarySubstrings(string s);
};

Examples

Example 1:

s = "00011"
Output: 2
Explanation: The valid ranges have text "01" and "0011".

Example 2:

s = "110100"
Output: 3
Explanation: Run lengths 2,1,1,2 contribute 1+1+1.

Constraints

  • Input length: 1..100000 characters. The alphabet contains only zero and one.

Clarifying Questions Guidance

Require exactly two adjacent equal-size character blocks and retain repeated occurrences.

Contract considerations

Require exactly two adjacent equal-size character blocks and retain repeated occurrences.