Given a binary string, count number of substrings that start and end with 1.

1.7

Given a binary string, count 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”.

Source: Amazon Interview Experience | Set 162

Difficulty Level: Rookie

A Simple Solution is to run two loops. Outer loops picks every 1 as starting point and inner loop searches for ending 1 and increments count whenever it finds 1.

C/C++

// A simple C++ program to count number of substrings starting and ending
// with 1
#include<iostream>
using namespace std;

int countSubStr(char str[])
{
   int res = 0;  // Initialize result

   // Pick a starting point
   for (int i=0; str[i] !='\0'; i++)
   {
        if (str[i] == '1')
        {
            // Search for all possible ending point
            for (int j=i+1; str[j] !='\0'; j++)
               if (str[j] == '1')
                  res++;
        }
   }
   return res;
}

// Driver program to test above function
int main()
{
  char str[] = "00100101";
  cout << countSubStr(str);
  return 0;
}

Java

// A simple C++ program to count number of 
//substrings starting and ending with 1

class CountSubString 
{
    int countSubStr(char str[],int n) 
    {
        int res = 0;  // Initialize result

        // Pick a starting point
        for (int i = 0; i<n; i++) 
        {
            if (str[i] == '1') 
            {
                // Search for all possible ending point
                for (int j = i + 1; j< n; j++) 
                {
                    if (str[j] == '1')
                        res++;
                }
            }
        }
        return res;
    }

    // Driver program to test the above function
    public static void main(String[] args) 
    {
        CountSubString count = new CountSubString();
        String string = "00100101";
        char str[] = string.toCharArray();
        int n = str.length;
        System.out.println(count.countSubStr(str,n));
    }
}


Output:

3

Time Complexity of the above solution is O(n2). We can find count in O(n) using a single traversal of input string. Following are steps.
a) Count the number of 1’s. Let the count of 1’s be m.
b) Return m(m-1)/2
The idea is to count total number of possible pairs of 1’s.

C/C++

// A O(n) C++ program to count number of substrings starting and ending
// with 1
#include<iostream>
using namespace std;

int countSubStr(char str[])
{
   int m = 0; // Count of 1's in input string

   // Travers input string and count of 1's in it
   for (int i=0; str[i] !='\0'; i++)
   {
        if (str[i] == '1')
           m++;
   }

   // Return count of possible pairs among m 1's
   return m*(m-1)/2;
}

// Driver program to test above function
int main()
{
  char str[] = "00100101";
  cout << countSubStr(str);
  return 0;
}

Java

// A O(n) C++ program to count number of substrings 
//starting and ending with 1

class CountSubString 
{
    int countSubStr(char str[], int n) 
    {
        int m = 0; // Count of 1's in input string

        // Travers input string and count of 1's in it
        for (int i = 0; i < n; i++) 
        {
            if (str[i] == '1')
                m++;
        }

        // Return count of possible pairs among m 1's
        return m * (m - 1) / 2;
    }<div id="company_tags"></div><div id="company_tags"></div>

    // Driver program to test the above function
    public static void main(String[] args) 
    {
        CountSubString count = new CountSubString();
        String string = "00100101";
        char str[] = string.toCharArray();
        int n = str.length;
        System.out.println(count.countSubStr(str, n));
    }
}


Output:

3

Asked in: Amazon

This article is contributed by Shivam. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1.7 Average Difficulty : 1.7/5.0
Based on 61 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.