- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

# Write a program in C++ to count the Number of substrings that starts with ‘1’ and ends with ‘1’

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 ’}.

## The approach used to solve this problem

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.

## Example

#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; }

## Output

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.

- 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
- Comparing ‘cubic’ and ‘linear’ 1-D interpolation using SciPy library
- 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++
- 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 subarrays with equal number of 1’s and 0’s in C++
- Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ 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