Count of sub-strings with equal consecutive 0’s and 1’s
Last Updated :
28 Dec, 2022
Given binary string str of 0’s and 1’s only. The task is to count the total number of substrings of string str such that each substring has an equal number of consecutive 0’s and 1’s in it.
Examples:
Input: str = “010011”
Output: 4
Explanation:
The substrings with consecutive 0’s and 1’s are “01”, “10”, “0011”, “01”. Hence, the count is 4.
Note:
The two “01” are at different positions: [0, 1] and [3, 4].
“010011” has the same number of 0’s and 1’s but they are not consecutive.
Input: str = “0001110010”
Output: 7
Explanation:
The substrings with consecutive 0’s and 1’s are “000111”, “0011”, “01”, “1100”, “10”, “01”, “10”.
Approach:
- Count the number of consecutive 0’s (or 1’s) from start of the string.
- Then count the number of consecutive 1’s (or 0’s) from the position in the string str where count of 0’s (or 1’s) ends.
- The total number of substrings with consecutive 0’s and 1’s is the minimum of the count of consecutive 0’s and 1’s found in above two steps.
- Repeat the above steps till the end of the string str.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSubstring(string& S, int & n)
{
int ans = 0;
int i = 0;
while (i < n) {
int cnt0 = 0, cnt1 = 0;
if (S[i] == '0' ) {
while (i < n && S[i] == '0' ) {
cnt0++;
i++;
}
int j = i;
while (j < n && S[j] == '1' ) {
cnt1++;
j++;
}
}
else {
while (i < n && S[i] == '1' ) {
cnt1++;
i++;
}
int j = i;
while (j < n && S[j] == '0' ) {
cnt0++;
j++;
}
}
ans += min(cnt0, cnt1);
}
return ans;
}
int main()
{
string S = "0001110010" ;
int n = S.length();
cout << countSubstring(S, n);
return 0;
}
|
C
#include <stdio.h>
#include <string.h>
int min( int num1, int num2)
{
return (num1 > num2) ? num2 : num1;
}
int countSubstring( char S[], int n)
{
int ans = 0;
int i = 0;
while (i < n) {
int cnt0 = 0, cnt1 = 0;
if (S[i] == '0' ) {
while (i < n && S[i] == '0' ) {
cnt0++;
i++;
}
int j = i;
while (j < n && S[j] == '1' ) {
cnt1++;
j++;
}
}
else {
while (i < n && S[i] == '1' ) {
cnt1++;
i++;
}
int j = i;
while (j < n && S[j] == '0' ) {
cnt0++;
j++;
}
}
ans += min(cnt0, cnt1);
}
return ans;
}
int main()
{
char S[] = "0001110010" ;
int n = strlen (S);
printf ( "%d" , countSubstring(S, n));
return 0;
}
|
Java
class GFG {
static int countSubstring(String S, int n)
{
int ans = 0 ;
int i = 0 ;
while (i < n) {
int cnt0 = 0 , cnt1 = 0 ;
if (S.charAt(i) == '0' ) {
while (i < n && S.charAt(i) == '0' ) {
cnt0++;
i++;
}
int j = i;
while (j < n && S.charAt(j) == '1' ) {
cnt1++;
j++;
}
}
else {
while (i < n && S.charAt(i) == '1' ) {
cnt1++;
i++;
}
int j = i;
while (j < n && S.charAt(j) == '0' ) {
cnt0++;
j++;
}
}
ans += Math.min(cnt0, cnt1);
}
return ans;
}
static public void main(String args[])
{
String S = "0001110010" ;
int n = S.length();
System.out.println(countSubstring(S, n));
}
}
|
Python3
def countSubstring(S, n) :
ans = 0 ;
i = 0 ;
while (i < n) :
cnt0 = 0 ; cnt1 = 0 ;
if (S[i] = = '0' ) :
while (i < n and S[i] = = '0' ) :
cnt0 + = 1 ;
i + = 1 ;
j = i;
while (j < n and S[j] = = '1' ) :
cnt1 + = 1 ;
j + = 1 ;
else :
while (i < n and S[i] = = '1' ) :
cnt1 + = 1 ;
i + = 1 ;
j = i;
while (j < n and S[j] = = '0' ) :
cnt0 + = 1 ;
j + = 1 ;
ans + = min (cnt0, cnt1);
return ans;
if __name__ = = "__main__" :
S = "0001110010" ;
n = len (S);
print (countSubstring(S, n));
|
C#
using System;
class GFG{
static int countSubstring( string S, int n)
{
int ans = 0;
int i = 0;
while (i < n) {
int cnt0 = 0, cnt1 = 0;
if (S[i] == '0' ) {
while (i < n && S[i] == '0' ) {
cnt0++;
i++;
}
int j = i;
while (j < n && S[j] == '1' ) {
cnt1++;
j++;
}
}
else {
while (i < n && S[i] == '1' ) {
cnt1++;
i++;
}
int j = i;
while (j < n && S[j] == '0' ) {
cnt0++;
j++;
}
}
ans += Math.Min(cnt0, cnt1);
}
return ans;
}
static public void Main ()
{
string S = "0001110010" ;
int n = S.Length;
Console.WriteLine(countSubstring(S, n));
}
}
|
Javascript
<script>
function countSubstring( S , n)
{
var ans = 0;
var i = 0;
while (i < n) {
var cnt0 = 0, cnt1 = 0;
if (S.charAt(i) == '0' ) {
while (i < n && S.charAt(i) == '0 ')
{
cnt0++;
i++;
}
// If consecutive 0' s
var j = i;
while (j < n && S.charAt(j) == '1' )
{
cnt1++;
j++;
}
}
else {
while (i < n && S.charAt(i) == '1 ')
{
cnt1++;
i++;
}
// If consecutive 1' s
var j = i;
while (j < n && S.charAt(j) == '0' )
{
cnt0++;
j++;
}
}
ans += Math.min(cnt0, cnt1);
}
return ans;
}
var S = "0001110010" ;
var n = S.length;
document.write(countSubstring(S, n));
</script>
|
Time Complexity: O(N), where N = length of string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...