- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Substrings that can be Made of Length 1 by Replacing "01" or "10" with 1 or 0
In this problem, we will count substrings that we can make of length 1 by replacing the β10β and β01β substrings with β1β or β0β characters.
When any binary string contains an equal number of β0β and β1β, we can always make it of length 1 by performing the replacement operations. So, the problem is solved by finding the substrings with equal numbers of β0β and β1β.
Problem statement β We have given a binary string named bin_str and of length bin_len. We need to count the total number of substrings that we can make of length 1 by changing β10β or β01β to either β1β or β0β.
Sample examples
Input
bin_str = "01010";
Output
6
Explanation β The substrings are 01, 01, 0101, 10, 10, and 1010.
Input
bin_str = "01";
Output
1
Explanation β We can replace 01 with β1β or β0β to make it of length 1.
Input
bin_str = "00000";
Output
0
Explanation β No substring exists, which we can make of length 1 after replacement.
Approach 1
The problem can be solved by counting the number of substrings having equal numbers of β1β and β0.β We will find all substrings of the string and count β1β and β0β in the string. If the count of β1β and β0β are the same, we will increment the βansβ by 1.
Algorithm
Step 1 β Initialize the βcntβ with 0 to store the number of valid substrings.
Step 2 β Also, initialize the βcnt0β and βcnt1β with 0 to store the count of β1β and β0β.
Step 3 β Start traversing the binary string.
Step 4 β Use a nested loop to traverse from index p to the last index.
Step 5 β If the current character is β0β, increment βcnt0β by 1. Otherwise, increment βcnt1β by 1.
Step 6 β If cnt0 and cnt1 are equal, increment the βcntβ value by 1.
Step 7 β Return the βcntβ value.
Example
#include <bits/stdc++.h> using namespace std; int totalSubstrs(string &bin_str, int &bin_len) { // To store a number of valid substrings int cnt = 0; // Couting number of substrings having equal 1's and 0's for (int p = 0; p < bin_len; p++) { // To store count of 1's and 0's in substring int cnt0 = 0, cnt1 = 0; for (int q = p; q < bin_len; q++) { if (bin_str[q] == '0') { cnt0++; } else { cnt1++; } // When the substring has an equal number of 0's and 1's if (cnt0 == cnt1) { cnt++; } } } return cnt; } int main() { string bin_str = "01010"; int bin_len = bin_str.length(); cout << "The number of substrings according to the given problem statement is " << totalSubstrs(bin_str, bin_len); return 0; }
Output
The number of substrings according to the given problem statement is 6
Time complexity β O(N*N) to traverse all substrings of the binary string.
Space complexity β O(1), as we donβt use any extra space.
Approach 2
In this approach, we will use the prefix sum technique to count the number of substrings having equal numbers of β1β and β0β. We will keep track of the difference of the count of β1β and β0β at every index.
If any two index difference of count of β1β and β0β is the same, we can take the substring between both indices.
Algorithm
Step 1 β Initialize the βcntβ and βdiffβ with 0 to store the count of valid substrings and the difference of count of β1β and β0β.
Step 2 β Also, initialize the βprefixSumβ list with 0 to store the difference.
Step 3 β Start traversing the string. If the current character is β0β, increment the βdiffβ value by 1. Otherwise, decrement the βdiffβ value by β1β.
Step 4 β If βdiffβ is β0β, increment the βcntβ by 1.
Step 5 β If the value in the βprefixSumβ array at the βdiffβ index is greater than 0, add prefixSum[diff] to the βcntβ.
Step 6 β Increment the value of the βprefixSumβ list at the βdiffβ index by 1.
Step 7 β Return the βcntβ value.
Example
#include <iostream> #include <vector> using namespace std; int totalSubstrs(string bin_str, int bin_len) { int cnt = 0; int diff = 0; // To store the difference of count of 1's and 0's vector<int> prefixSum(bin_len, 0); for (char ch : bin_str) { // For the '0' character if (ch == '0') { diff++; } else if (ch == '1') { // For '1' character diff--; } if (diff == 0) { // When the number of '0' and '1' are equal cnt++; } if (prefixSum[diff] > 0) { cnt += prefixSum[diff]; } prefixSum[diff]++; } return cnt; } int main() { string bin_str = "01010"; int bin_len = bin_str.length(); cout << "The number of substrings according to the given problem statement is - " << totalSubstrs(bin_str, bin_len); return 0; }
Output
The number of substrings according to the given problem statement is 6
Time complexity β O(N) to get the prefix sum of the difference of count of β1β and β0β.
Space complexity β O(N) to store the difference in the prefix sum list.
The problem is similar to finding the substrings with equal numbers of β1β and β0β. We have also used the prefix sum technique to solve the problem by storing the difference of the count of β1β and β0β in the prefix array. The second approach is time optimized, but it may take more memory for larger binary strings.