Count of substrings of a Binary string containing only 1s Last Updated : 16 Nov, 2023 Comments Improve Suggest changes Like Article Like Report 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 The_Approach: the approach simple we will going to traverse over array and count one follow the steps. initialize total=0 and currone=0. then traverse over string and count ones in string till zero occur. if zero occur then just add the number of possible substring that are possible from currone as. total+=(currone*(currone+1))/2. and after addition currone=0. do this till n-1.Below is the implementation of the above approach: C++ #include <bits/stdc++.h> #include <iostream> using namespace std; int main() { // Given string. string s = "0110111"; // to avoid int overflow. int mod = 1000000007; long long int total = 0; long long int one = 0; // traverse over string. for (int i = 0; i < s.size(); i++) { // count the ones. if (s[i] == '1') { ++one; } else { long long x = (one * (one + 1)) % mod; total += x / 2; one = 0; } } // count if reamin. if (one != 0) total += (long long)(one * (one + 1)) / 2; // printing the output. cout << total << endl; return 0; } Java import java.util.*; public class GFG { public static void main(String[] args) { // Given string. String s = "0110111"; // To avoid integer overflow. long mod = 1000000007; long total = 0; long one = 0; // Traverse over the string. for (int i = 0; i < s.length(); i++) { // Count the ones. if (s.charAt(i) == '1') { one++; } else { long x = (one * (one + 1)) % mod; total += x / 2; one = 0; } } // Count if remaining ones. if (one != 0) { total += (long) (one * (one + 1)) / 2; } // Printing the output. System.out.println(total); } } Python3 # Given string s = "0110111" # to avoid int overflow mod = 1000000007 total = 0 one = 0 # traverse over string for i in range(len(s)): # count the ones if s[i] == '1': one += 1 else: x = (one * (one + 1)) % mod total += x // 2 one = 0 # count if remain if one != 0: total += (one * (one + 1)) // 2 # printing the output print(total) C# using System; class Program { static void Main() { // Given string. string s = "0110111"; // to avoid int overflow. int mod = 1000000007; long total = 0; long one = 0; // traverse over string. for (int i = 0; i < s.Length; i++) { // count the ones. if (s[i] == '1') { ++one; } else { long x = (one * (one + 1)) % mod; total += x / 2; one = 0; } } // count if reamin. if (one != 0) total += (long)(one * (one + 1)) / 2; // printing the output. Console.WriteLine(total); } } // This code is contributed by user_dtewbxkn77n JavaScript // Given string. let s = "0110111"; // to avoid int overflow. let mod = 1000000007; let total = 0; let one = 0; // traverse over string. for (let i = 0; i < s.length; i++) { // count the ones. if (s[i] == '1') { ++one; } else { let x = (one * (one + 1)) % mod; total += x / 2; one = 0; } } // count if remain. if (one != 0) total += (one * (one + 1)) / 2; // printing the output. console.log(total); Output9 Complexity Analysis: Time Complexity : O(n). Auxiliary Space : O(1). 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 - 1Count 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++ // 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; } Java // 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 Python3 # 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 C# // 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 JavaScript <script> // Javascript implementation to find // count of substring containing // only ones // Function to find the total number // of substring having only ones function 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 Code var s = "0110111"; document.write( countOfSubstringWithOnlyOnes(s)); </script> Output9 Time Complexity: O(n), where n is the size of the given stringAuxiliary Space: O(1) Comment More infoAdvertise with us Next Article Count of substrings of a Binary string containing only 1s spp____ Follow Improve Article Tags : Strings Bit Magic Mathematical DSA binary-string substring +2 More Practice Tags : Bit MagicMathematicalStrings Similar Reads Count of substrings of a binary string containing K ones Given a binary string of length N and an integer K, we need to find out how many substrings of this string are exist which contains exactly K ones. Examples: Input : s = â10010â K = 1 Output : 9 The 9 substrings containing one 1 are, â1â, â10â, â100â, â001â, â01â, â1â, â10â, â0010â and â010âRecommen 7 min read Count N-length Binary Strings consisting of "11" as substring Given a positive integer N, the task is to find the number of binary strings of length N which contains "11" as a substring. Examples: Input: N = 2Output: 1Explanation: The only string of length 2 that has "11" as a substring is "11". Input: N = 12Output: 3719 Approach: The idea is to derive the num 8 min read Count of substrings in a Binary String that contains more 1s than 0s Given a binary string s, the task is to calculate the number of such substrings where the count of 1's is strictly greater than the count of 0's. Examples Input: S = "110011"Output: 11Explanation: Substrings in which the count of 1's is strictly greater than the count of 0's are { S[0]}, {S[0], S[1] 15+ min read Count of K length subarrays containing only 1s in given Binary String Given a binary string str, the task is to find the count of K length subarrays containing only 1s. Examples: Input: str = "0101000", K=1Output: 2Explanation: 0101000 -> There are 2 subarrays with 1 ones Input: str = "11111001", K=3Output: 3 Approach: The task can be solved by keeping track of the 4 min read Count ways to generate Binary String not containing "0100" Substring Given the number N, count the number of ways to create a binary string (the string that contains characters as zero or one) of size N such that it does not contain "0100" as a substring. A substring is a contiguous sequence of characters within a string. Examples: Input: N = 4Output: 15Explanation: 15+ min read Count of K length subarrays containing only 1s in given Binary String | Set 2 Given binary string str, the task is to find the count of K length subarrays containing only 1s. Examples Input: str = "0101000", K=1Output: 2Explanation: 0101000 -> There are 2 subarrays of length 1 containing only 1s. Input: str = "11111001", K=3Output: 3 Approach: The given problem can also be 4 min read Count number of binary strings of length N having only 0's and 1's Given an integer N, the task is to count the number of binary strings of length N having only 0's and 1's. Note: Since the count can be very large, return the answer modulo 10^9+7. Examples: Input: 2 Output: 4 Explanation: The numbers are 00, 01, 11, 10. Hence the count is 4. Input: 3 Output: 8 Expl 6 min read Count of substrings containing only the given character Given a string S and a character C, the task is to count the number of substrings of S that contains only the character C.Examples: Input: S = "0110111", C = '1' Output: 9 Explanation: The substrings containing only '1' are: "1" â 5 times "11" â 3 times "111" â 1 time Hence, the count is 9. Input: S 6 min read Count number of substrings of a string consisting of same characters Given a string. The task is to find out the number of substrings consisting of the same characters. Examples: Input: abba Output: 5 The desired substrings are {a}, {b}, {b}, {a}, {bb} Input: bbbcbb Output: 10 Approach: It is known for a string of length n, there are a total of n*(n+1)/2 number of su 6 min read XOR of all substrings of a given Binary String Given a binary string str of size N, the task is to calculate the bitwise XOR of all substrings of str. Examples: Input: str = "11"Output: 11Explanation: The substrings of "11" are: 1, 1, and 11.Their XOR = 1 â 1 â 11 = 11 Input: str = "110"Output: 111Explanation: The substrings of 110 are: 1, 1, 0, 6 min read Like