Count the Ones
Written by Fatima Amrat
Key Idea:
We are given a binary string S consisting of characters '0' and '1'. The task is to count the number of substrings (contiguous segments) that contain only the character '1'.
Any valid substring must lie entirely within a block of consecutive '1's. Therefore, instead of analyzing the whole string, it is enough to split it into maximal blocks of consecutive '1's and compute the contribution of each block separately.
Formula:
- If the start is at position 1, there are k possible endings.
- If the start is at position 2, there are k-1 possible endings.
- If the start is at position 3, there are k-2 possible endings. And so on, until we reach the start at position k, where there is exactly one ending.
So the total number of substrings in a block of length k is:
Algorithm:
Initialize variables
count = 0→ length of the current block of ones.ans = 0→ stores the total answer.
Traverse the string from left to right
- If the character is
'1', incrementcount. - If the character is
'0', add(count * (count + 1) / 2)toansand resetcount = 0.
- If the character is
After the loop
- Add the contribution of the last block of ones (in case the string ends with
'1').
- Add the contribution of the last block of ones (in case the string ends with
Output
- Print the value of
ans.
- Print the value of
Implementation (C++):
cpp
#include <bits/stdc++.h>
using namespace std ;
int main(){
string s ;
cin >> s ;
long long ans = 0 ;
long long count = 0 ;
for(int i = 0 ; i < s.size() ; i++){
if(s[i]=='1'){
count++;
}
else{
ans += count * (count + 1) / 2LL;
count = 0;
}
}
ans += count * (count + 1) / 2LL ;
cout << ans <<'\n' ;
}
