Count of substrings of a given Binary string with all characters same
Last Updated :
02 Jun, 2022
Given binary string str containing only 0 and 1, the task is to find the number of sub-strings containing only 1s and 0s respectively, i.e all characters same.
Examples:
Input: str = “011”
Output: 4
Explanation:
Three sub-strings are “1“, “1”, “11” which have only 1 in them, and one substring is there which contains only “0”.
Input: str = “0000”
Output: 10
Explanation:
There are no sub-strings having all ones in it.
Naive Approach: The idea is to generate all possible sub-string of the given string. For each sub-string check if the string contains all 1’s or all 0’s. If yes then count that sub-string. Print the count of sub-string after the above operations.
Time Complexity: O(N3)
Auxiliary Space: O(N)
Efficient Approach: The idea is to use the concept of Sliding window and Two pointers Approach. Below are the steps to find the count of the substring that contains only 1s:
- Initialize two pointers say L and R and initialize them to 0.
- Now iterate in the given string and check if the current character is equal to 1 or not. If it is, then extend the window by incrementing the value of R.
- If the current character is 0, then the window L to R – 1 contains all ones.
- Add the number of sub-strings of L to R – 1 to the answer that is ((R – L) * (R – L + 1)) / 2 and increment R and reinitialize L as R.
- Repeat the process until L and R cross each other.
- Print the count of all the substring in step 4.
- To count the number of substring with all 0s flip the given string i.e., all 0s will be converted into 1s and vice-versa.
- Repeat the above steps from step 1 to step 4 for character 1 in the flipped string to get a count of the substring that contains only 0s in it and print the count.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int countSubAllOnes(string s)
{
int l = 0, r = 0, ans = 0;
while (l <= r) {
if (r == s.length()) {
ans += ((r - l) * (r - l + 1)) / 2;
break ;
}
if (s[r] == '1' )
r++;
else {
ans += ((r - l) * (r - l + 1)) / 2;
l = r + 1;
r++;
}
}
return ans;
}
void flip(string& s)
{
for ( int i = 0; s[i]; i++) {
if (s[i] == '1' )
s[i] = '0' ;
else
s[i] = '1' ;
}
cout<<s<<endl;
}
int countSubAllZerosOnes(string s)
{
int only_1s = countSubAllOnes(s);
flip(s);
cout<<s<<endl;
int only_0s = countSubAllOnes(s);
return only_0s + only_1s;
}
int main()
{
string s = "011" ;
cout << countSubAllZerosOnes(s) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countSubAllOnes(String s)
{
int l = 0 , r = 0 , ans = 0 ;
while (l <= r)
{
if (r == s.length())
{
ans += ((r - l) * (r - l + 1 )) / 2 ;
break ;
}
if (s.charAt(r) == '1' )
r++;
else
{
ans += ((r - l) * (r - l + 1 )) / 2 ;
l = r + 1 ;
r++;
}
}
return ans;
}
static String flip( char []s)
{
for ( int i = 0 ; i < s.length; i++)
{
if (s[i] == '1' )
s[i] = '0' ;
else
s[i] = '1' ;
}
return String.valueOf(s);
}
static int countSubAllZerosOnes(String s)
{
int only_1s = countSubAllOnes(s);
s = flip(s.toCharArray());
int only_0s = countSubAllOnes(s);
return only_0s + only_1s;
}
public static void main(String[] args)
{
String s = "011" ;
System.out.print(countSubAllZerosOnes(s) + "\n" );
}
}
|
Python3
def countSubAllOnes(s):
l, r, ans = 0 , 0 , 0
while (l < = r):
if (r = = len (s)):
ans + = ((r - l) *
(r - l + 1 )) / / 2
break
if (s[r] = = '1' ):
r + = 1
else :
ans + = ((r - l) *
(r - l + 1 )) / / 2
l = r + 1
r + = 1
return ans
def flip(s):
arr = list (s)
for i in range ( len (s)):
if (arr[i] = = '1' ):
arr[i] = '0'
else :
arr[i] = '1'
s = ''.join(arr)
return s
def countSubAllZerosOnes(s):
only_1s = countSubAllOnes(s)
s = flip(s)
only_0s = countSubAllOnes(s)
return only_0s + only_1s
if __name__ = = "__main__" :
s = "011"
print (countSubAllZerosOnes(s))
|
C#
using System;
class GFG{
static int countSubAllOnes(String s)
{
int l = 0, r = 0, ans = 0;
while (l <= r)
{
if (r == s.Length)
{
ans += ((r - l) * (r - l + 1)) / 2;
break ;
}
if (s[r] == '1' )
r++;
else
{
ans += ((r - l) * (r - l + 1)) / 2;
l = r + 1;
r++;
}
}
return ans;
}
static String flip( char []s)
{
for ( int i = 0; i < s.Length; i++)
{
if (s[i] == '1' )
s[i] = '0' ;
else
s[i] = '1' ;
}
return String.Join( "" ,s);
}
static int countSubAllZerosOnes(String s)
{
int only_1s = countSubAllOnes(s);
s = flip(s.ToCharArray());
int only_0s = countSubAllOnes(s);
return only_0s + only_1s;
}
public static void Main(String[] args)
{
String s = "011" ;
Console.Write(countSubAllZerosOnes(s) + "\n" );
}
}
|
Javascript
<script>
function countSubAllOnes(s)
{
var l = 0, r = 0, ans = 0;
while (l <= r) {
if (r == s.length) {
ans += ((r - l) * (r - l + 1)) / 2;
break ;
}
if (s[r] == '1' )
r++;
else {
ans += ((r - l) * (r - l + 1)) / 2;
l = r + 1;
r++;
}
}
return ans;
}
function flip(s)
{
for ( var i = 0; s[i]; i++) {
if (s[i] == '1' )
s[i] = '0' ;
else
s[i] = '1' ;
}
return s;
}
function countSubAllZerosOnes(s)
{
var only_1s = countSubAllOnes(s);
s = flip(s.split( '' ));
var only_0s = countSubAllOnes(s);
return only_0s + only_1s;
}
var s = "011" ;
document.write( countSubAllZerosOnes(s));
</script>
|
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...