---
title: "Count Substrings with More 1's than 0's"
url: https://daily.dev/posts/count-substrings-with-more-1-s-than-0-s-k63z1kpmf
source_url: https://www.csharp.com/article/count-substrings-with-more-1s-than-0s
type: article
source: "C# Corner"
published: 2026-07-06T05:29:42.791Z
updated: 2026-07-06T05:30:06.338Z
tags: ["java", "algorithms"]
reading_time: 3
upvotes: 0
comments: 0
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Count Substrings with More 1's than 0's

**[C\# Corner](https://daily.dev/sources/csharpcorner)** · 3 min read · 0 upvotes · 0 comments

## Summary

A walkthrough of an O(N log N) algorithm to count binary string substrings with more 1s than 0s. The approach converts the problem by mapping 1→+1 and 0→-1, then uses prefix sums so that a valid substring corresponds to a pair where the later prefix sum exceeds the earlier one. Coordinate compression reduces the prefix sum range, and a Fenwick Tree efficiently counts how many previously seen prefix sums are smaller than the current one in O(log N) per query. A dry run, complexity table, and Java implementation are included.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.csharp.com/article/count-substrings-with-more-1s-than-0s>

## Similar posts on daily.dev

- [Cut Matrix: A Deep Dive into the DP \+ Binary Search Solution](https://daily.dev/posts/cut-matrix-a-deep-dive-into-the-dp-binary-search-solution-wmrbvf8lx) · C\# Corner · 1 upvotes · 0 comments
- [Equalize All Prefix Sums](https://daily.dev/posts/equalize-all-prefix-sums-1vho3hqp8) · C\# Corner · 0 upvotes · 0 comments

---

Tags: [#java](https://daily.dev/tags/java), [#algorithms](https://daily.dev/tags/algorithms)

[View this post on daily.dev](https://daily.dev/posts/count-substrings-with-more-1-s-than-0-s-k63z1kpmf)
