FastPrepCount Equal Binary Substrings

Count Equal Binary Substrings

JP Morgan Chase logoJP Morgan Chase● EasyOA
Learn

Problem statement

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

oneSubstringCount(s: String) → int

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

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 JP Morgan Chase problems

See JP Morgan Chase hiring insights
public int oneSubstringCount(String s) {
  // write your code here
}
s"011001"
expected4
Checking account…