FastPrepCount Binary Substrings
Problems/Count Binary Substrings

Count Binary Substrings

Yahoo logoYahoo● EasyOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

A binary string is a string consisting only of 0s and 1s. A substring is a contiguous group of characters within a string.

Given a binary string, find the number of substrings that contain an equal number of 0s and 1s and all the 0s and 1s are grouped together. Note that duplicate substrings are also counted in the answer. For example, '0011' has two overlapping substrings that meet the criteria: '0011' and '01'.

Function

getSubstringCount(s: String) → int

Complete the function getSubstringCount in the editor.

getSubstringCount has the following parameter(s):

  1. s: String: a binary string

Returns int: the number of substrings that meet the criteria

Examples

Example 1

s = "011001"return = 4

The substrings "01", "10", "1100", and "01" have equal numbers of 0s and 1s with all 0s and 1s grouped consecutively. Hence, the answer is 4. Note that the substring "0110" has an equal number of 0s and 1s but is not counted because not all 0s and 1s are grouped together.

Constraints

  • 1 ≤ length of s ≤ 105
  • The string s consists of 0s and 1s only.

More Yahoo interview questions

  • Logistic Regression with Gradient DescentPHONE SCREEN · Seen Nov 2026
  • Rank Unseen Items by Embedding SimilarityPHONE SCREEN · Seen Oct 2026
  • Modify the StringOA · Seen Mar 2025

Similar questions at other companies

  • Rearrange String by Alternating EndsHudson River Trading · OAHudson River Trading interview questions
  • Alternate String EndsCapital One · OACapital One interview questions
  • Run-Length String CompressionSalesforce · OASalesforce interview questions
  • Minimum Changes for Palindromic BlocksCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
  • Count Binary SubstringsMicrosoft · OAMicrosoft interview questions
  • Check a Repeated String as a SubsequenceQuince · OAQuince interview questions
  • Reverse String In PlaceThe Walt Disney Company · OA · PHONE SCREENThe Walt Disney Company interview questions
  • Lost MessagesSuperhuman · OASuperhuman interview questions
  • Reverse a StringFlexTrade · OAFlexTrade interview questions
  • Missing WordsVanguard · OAVanguard interview questions

Yahoo interview experiences

  • Yahoo Mail Senior MLE Interview Experience: 1 Experience
All Yahoo interview questionsSee Yahoo hiring insights
Saved
class Solution {
    public int getSubstringCount(String s) {
      // write your code here
    }

}
Checking account…