VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-of-substrings-in-a-binary-string-that-contains-more-1s-than-0s/

⇱ Count substrings that contain more 1s than 0s - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count substrings that contain more 1s than 0s

Last Updated : 14 Jun, 2026

Given a binary string s, the task is to calculate the number of such substrings where the count of 1's is strictly greater than the count of 0's. 

Examples

Input: s = "110011"
Output: 11
Explanation: Substrings in which the count of 1's is strictly greater than the count of 0's are {1}, {11}, {110}, {11001}, {110011}, {1}, {10011}, {011}, {1}, {11}, {1}.

Input: s = "011"
Output: 4
Explanation: There are 4 substring which has more 1s than 0s. i.e {011}, {1}, {11} and {1}.

Input: s = "0000"
Output: 0
Explanation: There is no substring with more 1s than 0s.

[Naive Approach] Generate all Substrings - O(n ^ 2) Time and O(1) Space

The simplest approach to solve the problem is to generate all substrings and count the number of 1s and 0s in each substring. Increase the count of those substrings that contain the count of 1s greater than the count of 0s. Finally, print the count obtained.

  • Start from every index i of the string and generate all possible substrings starting from i.
  • For each substring, maintain two counters: ones --> count of '1' and zeros --> count of '0'
  • Traverse the substring character by character and update the counts accordingly.
  • After adding each character, check whether ones > zeros. If yes, increment result by 1.

Output
11

[Expected Approach] Using Prefix Sum - O(n) Time and O(n) Space

The idea is to treat '1' as +1 and '0' as -1, so a substring with a positive sum has more 1s than 0s. We use prefix balances to avoid checking every substring separately. A frequency array stores how many times each balance occurs, while minus tracks invalid states where zeros are greater than or equal to ones. By updating these values dynamically, we count all valid substrings in linear time.

  • Treat every '1' as +1 and every '0' as -1.
  • Use a prefix sum concept where a positive difference means the substring has more 1s than 0s.
  • Store frequencies of prefix balances in the mp[] array.
  • Variable minus keeps track of invalid prefix states where zeros are greater than or equal to ones.
  • For every starting index, count valid substrings using:
    valid = total suffixes - invalid suffixes.
  • Update frequency counts and minus dynamically while moving the starting index forward.

Consider the following dry run for better understanding: s = "011"

  • Initial values: n = 3, zero = 3, cur = 3, minus = 0, ans = 0
  • Step 1: Build Prefix Frequencies
    For i = 0 --> s[0] = '0' --> cur = 2, cur <= zero so minus = 1, mp[2] = 1
    For i = 1 --> s[1] = '1' --> cur = 3, cur <= zero so minus = 2, mp[3] = 1
    For i = 2 --> s[2] = '1' --> cur = 4, minus = 2, mp[4] = 1
    Final State: mp[2] = 1, mp[3] = 1, mp[4] = 1, zero = 3, minus = 2, ans = 0
  • Step 2: Count Valid Substrings
    For i = 0 --> s[0] = '0' --> ans += (3 - 0 - 2) = 1 --> ans = 1, mp[2]-- = 0, zero = 2,
    minus-- = 1, minus -= mp[3](1) = 0
    For i = 1 --> s[1] = '1' --> ans += (3 - 1 - 0) = 2 --> ans = 3, mp[3]-- = 0, zero = 3, minus += mp[3](0) = 0
    For i = 2 --> s[2] = '1' --> ans += (3 - 2 - 0) = 1 --> ans = 4, mp[4]-- = 0, zero = 4, minus += mp[4](0) = 0

Final answer : ans = 4. Valid Substrings : {011}, {1}, {11} and {1}.


Output
11
Comment