Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Write a program in C++ to count the Number of substrings that starts with ‘1’ and ends with ‘1’
We are given the length of a binary string str and the string itself. The task is to count the number of substrings that start with '1' and end with '1'. A binary string contains only '0's and '1's, and a substring is any continuous part of the given string.
Let's look at a few example scenarios to understand the problem clearly:
Scenario 1
<b>Input: </b>N = 5, str = "11101"
<b>Output:</b> 6
<b>Explanation:
</b>In the given binary string, there are 6 substrings that start and end with '1'.
These are: ("11", "111", "1110", "11101", "1101", and "101").
Scenario 2
<b>Input:</b> N = 4, str = "0011"
<b>Output: </b>1
Explanation:
In this binary string, there's only one substring that starts and ends with '1':
("11") So, the total count is 1.
Approach to Solve the Problem
The problem is similar to the well-known Handshake Problem, where we count the number of handshakes in a party of n people. Each person shakes hands with every other person once, so the total number of handshakes is n x (n - 1) / 2.
Similarly, if we count the number of 1's in the given string, we can find the number of substrings that start and end with '1'.
Here are the steps we followed:
- First, we define a function countSubstring(int N, const string& s) that takes the length of the string and the binary string as input, and returns the count of substrings that start and end with '1'.
- Next, we loop through the string and count the number of '1's.
- Then, we apply the formula n x (n - 1) / 2 using the count of '1's.
- Finally, we return the result of that calculation.
C++ Program to Count Substrings Starting and Ending with '1'
Here is a C++ program that uses the above steps to count all substrings that start and end with '1'.
#include<iostream>
using namespace std;
int countSubstring(int N, const string& s) {
int count = 0;
for(int i = 0; s[i] != '\0'; ++i) {
if(s[i] == '1')
count++;
}
// Using the handshake formula to count valid pairs of '1'
return count * (count - 1) / 2;
}
int main() {
int N = 5;
string str = "11101";
cout << "Input: N = " << N << ", String = " << str << endl;
int result = countSubstring(N, str);
cout << "Count of substrings: " << result << endl;
return 0;
}
Following is the output of the above program:
Input: N = 5, String = 11101 Count of substrings: 6
Time Complexity: O(N), where N is the length of the input string s.
Space Complexity: O(1), no extra space is used.
Conclusion
In this article, we learned how to count the number of substrings that start and end with '1' in a binary string by counting the total number of '1's and applying the formula n x (n - 1) / 2. The program runs with a time complexity of O(n) and a space complexity of O(1).
