Count of substrings of a Binary string containing only 1s

Given a binary string of length N, we need to find out how many sub-strings of this string contains only 1s.

Examples: 

Input: S = “0110111”
Output: 9
Explanation:
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 one’s 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 ones 
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 Code 
int main() 
    string s = "0110111"
    cout << countOfSubstringWithOnlyOnes(s) 
        << endl; 
    return 0; 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to find 
// count of substring containing 
// only ones 
class GFG{ 
      
// Function to find the total number 
// of substring having only ones 
static 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 code 
public static void main(String[] args) 
    String s = "0110111"
      
    System.out.println(countOfSubstringWithOnlyOnes(s)); 
  
// This code is contributed by dewantipandeydp 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to find 
# count of substring containing 
# only ones 
  
# Function to find the total number 
# of substring having only ones 
def 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 Code 
s = "0110111"
print(countOfSubstringWithOnlyOnes(s)) 
  
# This code is contributed by jojo9911 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to find count
// of substring containing only ones 
using System;
  
class GFG{ 
      
// Function to find the total number 
// of substring having only ones 
static 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 code 
public static void Main(string[] args) 
    string s = "0110111"
      
    Console.Write(countOfSubstringWithOnlyOnes(s)); 
  
// This code is contributed by rutvik_56

chevron_right


Output

9

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.