Dense sub-string: number of 1’s > number of 0’s
Brute force approach in C++
#include <iostream>
#include <string>
using namespace std;
int bin_dense_count_bruteforce(string s) {
int n = s.size();
int ans = 0;
for(int i=0; i<=n-1; i++) {
int statusNow = 0;
for(int j=i; j<=n-1; j++) {
if(s[j] == '1')
statusNow++;
else
statusNow--;
if(statusNow>0)
ans++;
}
}
return ans;
}
int main() {
string s = "11000101";
cout<<bin_dense_count_bruteforce(s)<<endl;
// 7
// i, j substring
// 0, 0 1
// 0, 1 11
// 0, 2 110
// 1, 1 1
// 5, 5 1
// 5, 7 101
// 7, 7 1
return 0;
}
I tried thinking about any approach with unordered_map, like prefix sum like approach or similar way. I have tried searching online and even chatgpt, claude, etc. but no success.