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
arvind16yadav3 days ago
JAVA   Solution              Time : 0.67/1.82
class Solution
{
    //Function to count the number of substrings that start and end with 1.
    public static int binarySubstring(int a, String str)
    { 
        // Your code here
        String ans = str.replaceAll("0", "");
        int n = ans.length();
        return n * (n-1) / 2 ;
    }
}
0
007singhsanjeevkumar1 week ago
#simple code 0.02s   
 def binarySubstring(self,n,s):       #code here       c=s.count('1')       return int(c*(c-1)/2)
0
17_7_32 weeks ago
long binarySubstring(int n, string a){
       int count=0;
       for(int i=0;i<n;i++){
           if(a[i]=='1'){
               count++;
           }
       }
       return count*(count-1)/2;
   }
0
eramanchoudhary2 weeks ago
Easiest C++ solution 
solution time:-0.03/1.12
 
 int count =0;     int ans =0;     for(int i=0;i<n;i++){         if(a[i]=='1'){             ans+=count;             count++;         }     }     return ans;
0
eramanchoudhary
This comment was deleted.
+3
tarunjoshiji13 weeks ago
simplest C++ solution time:- 0.04/1.12
long i=0,ans=0;       for(int j=0;j<n;j++)       {           if(a[j]=='1')           {               ans+=i;               i++;           }       }       return ans;
+1
nikhilgoyal1453 weeks ago
class Solution{   //Function to count the number of substrings that start and end with 1.   public static int binarySubstring(int a, String str)   {       /* Method 1       if(str.equals("") || str == null) return 0;              int count = 0;       int base = 3;       for(int i=0;i<str.length();i++)       {           if(str.charAt(i) == '1')           {               if(str.lastIndexOf("1") > i)               {                   count++;                   if(count >= 3)                   {                       if(count != 3)                           count--;                       count += base;                       base++;                   }               }           }       }       if(base == 3 && count > 1)           count++;       return count;       */              //Method 2 (Formula Approach)       if(str == null || str.length() <=1 ) return 0;              int count = 0;       for(int i=0;i<str.length();i++)       {           if(str.charAt(i) == '1')               count++;       }       return count * (count-1)/2;   }}
0
thakursahabsingh123 weeks ago
JAVA :
 
 
class Solution{   //Function to count the number of substrings that start and end with 1.   public static int binarySubstring(int a, String str)   {       // Your code here                int c=0;      for(int i=0;i<str.length();i++){          if(str.charAt(i)=='1')          c++;      }      return c*(c-1)/2;         }}
0
shubhamrikhari86513 weeks ago
long binarySubstring(int n, string a){       long count = 0;       for(int i=0; i<a.size(); i++) {           if(a[i] == '1') {               count++;           }       }       return count*(count-1)/2;   }
0
shivamverma2001sj3 weeks ago
public static int binarySubstring(int a, String str)   {       // Your code here       int c=0;       for(int i=0;i<str.length();i++){           if(str.charAt(i)=='1')           c++;       }       return c*(c-1)/2;   }
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.