Problem · String

Count Equal Binary Substrings

EasyJPMorgan ChaseOA

Count the number of substrings in a binary string that contain an equal number of 0s and 1s, where all 0s and 1s are grouped together. Duplicate substrings are counted in the total.

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

Function Description

Complete the function oneSubstringCount in the editor within the following parameter(s):

  • s: a binary string

Returns

int: the number of substrings that meet the criteria

Endless gratitude to a dear old friend whose generosity in sharing the source made all the difference. 🌸

Examples
01 · Example 1
s = "011001"
return = 4
The qualifying substrings are "01", "10", "1100", and "01", giving a total of 4. Note that "0110" has equal 0s and 1s but is not counted because the 0s and 1s are not grouped together.
More JPMorgan Chase problems
drafts saved locally
public int oneSubstringCount(String s) {
  // write your code here
}
s"011001"
expected4
sign in to submit