Binary String | Practice | GeeksforGeeks

Given a binary string S. The task is to count the number of substrings that start and end with 1. For example, if the input string is “00100101”, then there are three substrings “1001”, “100101” and “101”.
Example 1:
Input:
N = 4
S = 1111
Output: 6
Explanation: There are 6 substrings from
the given string. They are 11, 11, 11,
111, 111, 1111.
Example 2:
Input:
N = 5
S = 01101
Output: 3
Explanation: There 3 substrings from the
given string. They are 11, 101, 1101.
Your Task:
The task is to complete the function binarySubstring() which takes the length of binary string n and a binary string a as input parameter and counts the number of substrings starting and ending with 1 and returns the count.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 104
+1
divyabhatia5191 week ago
public static int binarySubstring(int a, String str)   {       // Your code here      int count=0;      for(int i=0;i<a;i++){          if(str.charAt(i)=='1'){              count++;          }      }            return (count*(count-1))/2;   }
+1
visitant1 week ago
If you realize it is a combinatorics problem then you just need to count number of ‘1’s and return nC2 = n * (n-1)/2;
long binarySubstring(int n, string a){
        
        // Your code here
        int count = 0;
        for(int i=0; i<n ; i++)
        {
            if(a[i]=='1')
            {
                count++;
            }
        }
        return count*(count-1)/2;
        
    }
+2
sunghunet1 month ago
    # More pythonic way
    # It takes 0.0 / 1.0 [Total Time Taken].
    def binarySubstring(self,n,s):
        cnt = s.count('1')
        return cnt*(cnt-1) // 2
+1
jagannath23sahoo1 month ago
    //Function to count the number of substrings that start and end with 
    long binarySubstring(int n, string a){
        
        long c = 0;
        //We need to select 2 1s out of count so nC2 n(n-1)/2;
        for(auto x : a)
        {
            if(x == '1')    
                c++;
        }
        return (c*(c-1))/2;
        
    }
+1
anuragraj5731 month ago
PYTHON SOLUTION:
class Solution:   def binarySubstring(self,n,s):       count = 0       for i in range(n):           if(s[i]=='1'):               count=count+1       return (count*(count-1))//2
0
rajhans1 month ago
its based on maths selection problem. in simple words we have to select two 1s out of total count for which maths formula is n C 2 = n(n-1)/2       int count=0;       for(int i=0;i<a;i++){           if(str.charAt(i)=='1'){               count++;           }       }              return (count*(count-1))/2;
+3
parthmalhotra2 months ago
JAVA Solution
Approach : If we consider an index i of the string where character at the index i is 1, all the 1s before it will contribute a substring that ends at the current index i. 
So we keep counting the no of 1s occurring before the current index (i.e. the substrings ending at current index) and add that value to the result.
 
public static int binarySubstring(int a, String str)
    {
        //the result 
        int res = 0;
        
        //count variable specifying how many 1s are there before the given 1
        //initializing as -1, since for the first 1, value will become 0 on incrementing count
        int count = -1;
        
        //looping over the string
        for(int i= 0; i<a; i++){
            //if char '1' is found
            if(str.charAt(i)=='1'){
                //increment count
                count++;
                //add count to result
                res+=count;
            }
        }
        
        return res;
    }
0
singhdipranjan672 months ago
class Solution
{
    //Function to count the number of substrings that start and end with 1.
    public static int binarySubstring(int a, String str)
    {
       int count=0;
       for(int i=0; i<a; i++)
       {
           if(str.charAt(i)=='1')
           count++;
       }
      return count*(count-1)/2;
        
    }
}
0
ultradluffy82 months ago
based on discrete mathematics :
long binarySubstring(int n, string a){
        
        // Your code here
        int cnt=0;
        for(auto x:a){
            if(x == '1')cnt++;
        }
        return (cnt * (cnt-1))/2;
    }
0
tarunkanade2 months ago
0.5/1.8
public static int binarySubstring(int a, String str)
    {
        // Your code here
        int count1 = 0, res = 0;
        
        for(int i = 0; i < a; i++){
            if(str.charAt(i) == '1'){
                res += count1;
                count1++;
            }
        }
        return res;
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.