Count of substrings of a Binary string containing only 1s - GeeksforGeeks

30 Mar, 2022
Given a binary string of length N, we need to find out how many substrings of this string contain only 1s.
Examples: 
Input: S = “0110111”Output: 9Explanation:There are 9 substring with only 1’s characters. “1” comes 5 times. “11” comes 3 times. “111” comes 1 time.
Input: S = “000”Output: 0
Approach: The idea is to traverse the binary string and count the consecutive ones in the string. Below is the illustration of the approach:
Traverse the given binary string from index 0 to length – 1
Count the number of consecutive “1” till index i.
For each new character str[i], there will be more substring with all character’s as “1”
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ implementation to find// count of substring containing// only ones #include <bits/stdc++.h>using namespace std; // Function to find the total number// of substring having only onesint countOfSubstringWithOnlyOnes(string s){    int res = 0, count = 0;    for (int i = 0; i < s.length(); i++) {    count = s[i] == '1' ? count + 1 : 0;    res = (res + count);    }    return res;} // Driver Codeint main(){    string s = "0110111";    cout << countOfSubstringWithOnlyOnes(s)        << endl;    return 0;}
// Java implementation to find// count of substring containing// only onesclass GFG{     // Function to find the total number// of substring having only onesstatic int countOfSubstringWithOnlyOnes(String s){    int res = 0, count = 0;    for(int i = 0; i < s.length(); i++)    {        count = s.charAt(i) == '1' ? count + 1 : 0;        res = (res + count);    }    return res;} // Driver codepublic static void main(String[] args){    String s = "0110111";         System.out.println(countOfSubstringWithOnlyOnes(s));}} // This code is contributed by dewantipandeydp
# Python3 implementation to find# count of substring containing# only ones # Function to find the total number# of substring having only onesdef countOfSubstringWithOnlyOnes(s):     count = 0    res = 0         for i in range(0,len(s)):        if s[i] == '1':            count = count + 1        else:            count = 0;                     res = res + count                 return res # Driver Codes = "0110111"print(countOfSubstringWithOnlyOnes(s)) # This code is contributed by jojo9911
// C# implementation to find count// of substring containing only onesusing System; class GFG{     // Function to find the total number// of substring having only onesstatic int countOfSubstringWithOnlyOnes(string s){    int res = 0, count = 0;         for(int i = 0; i < s.Length; i++)    {        count = s[i] == '1' ? count + 1 : 0;        res = (res + count);    }    return res;} // Driver codepublic static void Main(string[] args){    string s = "0110111";         Console.Write(countOfSubstringWithOnlyOnes(s));}} // This code is contributed by rutvik_56
<script> // Javascript implementation to find// count of substring containing// only ones // Function to find the total number// of substring having only onesfunction countOfSubstringWithOnlyOnes(s){    var res = 0, count = 0;    for (var i = 0; i < s.length; i++) {    count = s[i] == '1' ? count + 1 : 0;    res = (res + count);    }    return res;} // Driver Codevar s = "0110111";document.write( countOfSubstringWithOnlyOnes(s)); </script>
9
Time Complexity: O(n), where n is the size of the given stringAuxiliary Space: O(1)
dewantipandeydp
jojo9911
rutvik_56
itsok
singhh3010
binary-string
substring
Bit Magic
Mathematical
Strings
Strings
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Check whether K-th bit is set or not
Program to find parity
Write an Efficient Method to Check if a Number is Multiple of 3
Hamming code Implementation in C/C++
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7