Problem

Given a binary string s, count all substrings that contain more 1s than 0s.

Example

Input:

Code
s = "011"

Output:

Code
4

Explanation

The valid substrings are:

  • "011"

  • "1"

  • "11"

  • "1"

Brute Force Approach

Generate every possible substring and count the number of 0s and 1s.

Code
for every starting index
    for every ending index
        count 0 and 1
        if (ones > zeros)
            answer++

Complexity

  • Time: O(N³) (or O(N²) with prefix counts)

  • Space: O(1)

This approach is too slow for N = 60000.

Optimized Idea

Instead of counting zeros and ones separately for every substring, convert the problem into a prefix sum problem.

Step 1: Convert Characters

Replace:

Code
1 → +1
0 → -1

Example

Code
0110

becomes

Code
-1 +1 +1 -1

Step 2: Prefix Sum

Create prefix sums.

Code
Prefix[0] = 0

Prefix[i] = sum of first i characters

Example

Code
String : 0 1 1

Value  : -1 +1 +1

Prefix array:

Code
Index : 0   1   2   3
Value : 0  -1   0   1

Step 3: Substring Sum

For a substring:

Code
l...r

its sum is:

Code
Prefix[r + 1] - Prefix[l]

If:

Code
sum > 0

then:

Code
ones > zeros

Therefore:

Code
Prefix[r + 1] > Prefix[l]

So, for every current prefix sum, we only need to know:

How many previous prefix sums are smaller than the current prefix sum?

This becomes the entire problem.

Why Fenwick Tree?

Suppose the current prefix sum is:

Code
5

We need to count how many previously seen prefix sums are:

Code
-3
-2
0
1
2
4

A Fenwick Tree can efficiently answer:

How many values are smaller than x?

in:

Code
O(log N)

time.

Coordinate Compression

Prefix sums can range from:

Code
-N to +N

For:

Code
N = 60000

the range becomes:

Code
-60000 ... 60000

Instead of creating a huge Fenwick Tree, compress the values.

Example

Original values:

Code
-5
2
8
0

After sorting:

Code
-5
0
2
8

Assign ranks:

Code
-5 → 1
0  → 2
2  → 3
8  → 4

The Fenwick Tree stores only these ranks.

Code Explanation

Solution Class

Java
class Solution {

This is the main solution class.

Fenwick Tree

Java
class Fenwick {

The Fenwick Tree supports:

  • Update

  • Prefix query

Variables

Java
int[] bit;
int n;
  • bit stores frequencies.

  • n is the size of the tree.

Constructor

Java
Fenwick(int n) {
    this.n = n;
    bit = new int[n + 2];
}

Creates the Fenwick Tree.

Update

Java
void update(int idx, int val) {
    while (idx <= n) {
        bit[idx] += val;
        idx += idx & -idx;
    }
}

Adds one occurrence of a prefix sum.

Example

Current prefix sum:

Code
3
Java
update(rank(3), 1);

Now the Fenwick Tree remembers that prefix sum 3 has appeared once.

Query

Java
int query(int idx) {
    int sum = 0;
    while (idx > 0) {
        sum += bit[idx];
        idx -= idx & -idx;
    }
    return sum;
}

Returns the number of prefix sums whose rank is less than or equal to idx.

Build Prefix Array

Java
int[] pref = new int[n + 1];
pref[0] = 0;

The prefix sum always starts with zero.

Build Prefix Sums

Java
for (int i = 1; i <= n; i++) {
    pref[i] = pref[i - 1] + (s.charAt(i - 1) == '1' ? 1 : -1);
}

If the current character is:

Code
1

add:

Code
+1

Otherwise, add:

Code
-1

Example

For:

Code
011

The prefix sums are:

Code
0
-1
0
1

Coordinate Compression

Copy the prefix array:

Java
int[] arr = pref.clone();

Sort it:

Java
Arrays.sort(arr);

Create a mapping:

Java
HashMap<Integer, Integer> map = new HashMap<>();

Assign ranks:

Java
int idx = 1;

for (int x : arr) {
    if (!map.containsKey(x))
        map.put(x, idx++);
}

Example

Prefix sums:

Code
0
-1
0
1

Assigned ranks:

Code
-1 → 1
0  → 2
1  → 3

Main Logic

Java
long ans = 0;

Stores the final answer.

Process Every Prefix Sum

Java
for (int x : pref) {

The current prefix sum is:

Code
x

Find its rank:

Java
int pos = map.get(x);

Count all smaller prefix sums:

Java
ans += ft.query(pos - 1);

Why?

Because:

Code
current prefix > previous prefix

means:

Code
substring sum > 0

which means:

Code
ones > zeros

Insert the current prefix sum:

Java
ft.update(pos, 1);

Now future prefix sums can use it.

Finally:

Java
return (int) ans;

Dry Run

Input

Code
011

Converted values:

Code
-1 +1 +1

Prefix sums:

Code
0
-1
0
1

Compressed ranks:

Code
-1 → 1
0  → 2
1  → 3

Initially, the Fenwick Tree is empty.

Prefix = 0

Code
query(1) = 0

answer = 0

insert 0

Prefix = -1

Code
query(0) = 0

answer = 0

insert -1

Prefix = 0

Code
query(1) = 1

answer = 1

insert 0

Prefix = 1

Code
query(2) = 3

answer = 4

insert 1

Final answer:

Code
4

Complexity Analysis

OperationComplexity
Prefix SumO(N)
SortingO(N log N)
Coordinate CompressionO(N)
Fenwick Tree QueriesO(N log N)

Time Complexity

Code
O(N log N)

Space Complexity

Code
O(N)

Java Implementation

Java
class Solution {

    class Fenwick {

        int[] bit;
        int n;

        Fenwick(int n) {
            this.n = n;
            bit = new int[n + 2];
        }

        void update(int idx, int val) {
            while (idx <= n) {
                bit[idx] += val;
                idx += idx & -idx;
            }
        }

        int query(int idx) {
            int sum = 0;
            while (idx > 0) {
                sum += bit[idx];
                idx -= idx & -idx;
            }
            return sum;
        }
    }

    public int countSubstring(String s) {

        int n = s.length();

        int[] pref = new int[n + 1];
        pref[0] = 0;

        for (int i = 1; i <= n; i++) {
            pref[i] = pref[i - 1] + (s.charAt(i - 1) == '1' ? 1 : -1);
        }

        // Coordinate Compression
        int[] arr = pref.clone();
        java.util.Arrays.sort(arr);

        java.util.HashMap<Integer, Integer> map = new java.util.HashMap<>();

        int idx = 1;

        for (int x : arr) {
            if (!map.containsKey(x))
                map.put(x, idx++);
        }

        Fenwick ft = new Fenwick(idx);

        long ans = 0;

        for (int x : pref) {

            int pos = map.get(x);

            // Count previous prefix sums strictly smaller
            ans += ft.query(pos - 1);

            ft.update(pos, 1);
        }

        return (int) ans;
    }
}

Key Insight

The problem is transformed from counting 1s and 0s in every substring to comparing prefix sums.

  • Convert 1 → +1 and 0 → -1.

  • A substring has more 1s than 0s if its sum is positive.

  • A positive substring sum means the current prefix sum is greater than an earlier prefix sum.

  • Use a Fenwick Tree to efficiently count how many earlier prefix sums are smaller than the current one, resulting in an O(N log N) solution.

Summary

By converting the binary string into a sequence of +1 and -1 values, the problem becomes one of comparing prefix sums instead of repeatedly counting characters. Every valid substring corresponds to a pair of prefix sums where the later prefix is greater than the earlier one. Coordinate compression reduces the prefix sum range, while a Fenwick Tree efficiently counts previously seen smaller prefix sums in O(log N) time per operation. This optimization improves the overall complexity from brute force to O(N log N) with O(N) space.