- Related Questions & Answers
- Program to build DFA that starts and ends with ‘a’ from the input (a, b)
- Validate input: replace all ‘a’ with ‘@’ and ‘i’ with ‘!’JavaScript
- Program to build DFA that starts and end with ‘a’ from input (a, b) in C++
- Count substrings that starts with character X and ends with character Y in C++
- Count subarrays with equal number of 1’s and 0’s in C++
- What is the use of ‘ALL’, ‘ANY’, ’SOME’, ’IN’ operators with MySQL subquery?
- Convert all substrings of length ‘k’ from base ‘b’ to decimal in C++
- Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time in C++
- How to Use ‘cat’ and ‘tac’ Commands with Examples in Linux
- Program to print ‘N’ alphabet using the number pattern from 1 to n in C++
- Are ‘this’ and ‘super’ keywords in Java?
- ‘AND’ vs ‘&&’ operators in PHP
- Replacing ‘public’ with ‘private’ in “main” in Java
- Why in MySQL, we cannot use arithmetic operators like ‘=’, ‘<’ or ‘<>’ with NULL?
- PHP program to find the sum of first n natural numbers that are divisible by a number ‘x’ or a number ‘y’

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Let’s suppose we have given a length of string ‘str’ and a string. The task is to count the number of substrings that starts with ‘1’ and ends with ‘1’ in a given Binary String. A Binary String contains ‘1’ and ‘0’ only. For example,

**Input-1** −

N = 5 str = ‘11101’

**Output** −

6

**Explanation** − In the given Binary String, we have 6 substrings that starts with ‘1’ and ends with ‘1’. The set of these substrings are {‘11’, ‘111’, ‘1110’, ‘11101’, ‘1101’, ‘101’}.

**Input-1** −

N = 4 str = ‘0011’

**Output** −

1

**Explanation** −

In the given Binary String we have 1 substring that starts with ‘1’ and ends with ‘1’. The set of these substrings is a singleton set i.e., { ‘11 ’}.

For the given string, we have to count the number of substrings that start with ‘1’ and end with ‘1’. The problem is similar to the well-known Handshake problem in which we have to count the number of handshakes in a party of ‘n’ people.

If we count the number of 1’s in the given string, then we can find the set of substrings that starts with ‘1’ and ends with ‘1’.

Take Input a string of N length.

An Integer function countSubstring(int N, string s) takes the length of the string and a string as input and returns the count of all the substring that starts with ‘1’ and ends with ‘1’.

Iterate over the whole string and count the no. of ‘1’ in the string.

Count the no. of substring (pair) in the given string by n*(n-1)/2.

Return the result of n*(n-1)/2.

#include<iostream> using namespace std; int countSubstring(int N, string s){ int count=0; for(int i=0; s[i]!= '\0'; ++i){ if( s[i]== '1' ) count++; } return count*(count-1)/2; } int main() { int N=5; string str= "11101"; cout<< countSubstring(N,str)<<endl; return 0; }

If we will run the above code then it will print the output as,

6

Since the number of ‘1’s present in the given string is ‘4’ i.e., count = 4, the total number of the substring that starts with ‘1’ and ends with ‘1’ are, 4*(4-1)/2 = 6.

Advertisements