Count of non-overlapping sub-strings “101” and “010” in the given binary string
Last Updated :
07 Oct, 2022
Given binary string str, the task is to find the count of non-overlapping sub-strings of either the form “010” or “101”.
Examples:
Input: str = “10101010101”
Output: 3
str[0..2] = “101”
str[3..5] = “010”
str[6..8] = “101”
Input: str = “111111111111110”
Output: 0
Approach: Initialize count = 0 and for every index i in the given string check whether the sub-string of size 3 starting at the current index i matches either with “010” or “101”. If it’s a match then update count = count + 1 and i = i + 3 (to avoid overlapping of sub-strings) else increment i by 1.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int countSubStr(string &s, int n)
{
int count = 0;
for ( int i = 0; i < n - 2;) {
if (s[i] == '0' && s[i + 1] == '1'
&& s[i + 2] == '0' ) {
count++;
i += 3;
}
else if (s[i] == '1' && s[i + 1] == '0'
&& s[i + 2] == '1' ) {
count++;
i += 3;
}
else {
i++;
}
}
return count;
}
int main()
{
string s = "10101010101" ;
int n = s.length();
cout << countSubStr(s, n);
return 0;
}
|
Java
class GFG
{
static int countSubStr( char [] s, int n)
{
int count = 0 ;
for ( int i = 0 ; i < n - 2 😉
{
if (s[i] == '0' && s[i + 1 ] == '1'
&& s[i + 2 ] == '0' )
{
count++;
i += 3 ;
}
else if (s[i] == '1' && s[i + 1 ] == '0'
&& s[i + 2 ] == '1' )
{
count++;
i += 3 ;
}
else
{
i++;
}
}
return count;
}
public static void main(String[] args)
{
char [] s = "10101010101" .toCharArray();
int n = s.length;
System.out.println(countSubStr(s, n));
}
}
|
Python3
def countSubStr(s, n) :
count = 0 ;
i = 0
while i < (n - 2 ) :
if (s[i] = = '0' and s[i + 1 ] = = '1' and s[i + 2 ] = = '0' ) :
count + = 1 ;
i + = 3 ;
elif (s[i] = = '1' and s[i + 1 ] = = '0' and s[i + 2 ] = = '1' ) :
count + = 1 ;
i + = 3 ;
else :
i + = 1 ;
return count;
if __name__ = = "__main__" :
s = "10101010101" ;
n = len (s);
print (countSubStr(s, n));
|
C#
using System;
class GFG
{
static int countSubStr( char [] s, int n)
{
int count = 0;
for ( int i = 0; i < n - 2;)
{
if (s[i] == '0' &&
s[i + 1] == '1' &&
s[i + 2] == '0' )
{
count++;
i += 3;
}
else if (s[i] == '1' &&
s[i + 1] == '0' &&
s[i + 2] == '1' )
{
count++;
i += 3;
}
else
{
i++;
}
}
return count;
}
public static void Main(String[] args)
{
char [] s = "10101010101" .ToCharArray();
int n = s.Length;
Console.WriteLine(countSubStr(s, n));
}
}
|
Javascript
<script>
function countSubStr( s , n) {
var count = 0;
for (i = 0; i < n - 2;) {
if (s[i] == '0' && s[i + 1] == '1' && s[i + 2] == '0' ) {
count++;
i += 3;
}
else if (s[i] == '1' && s[i + 1] == '0' && s[i + 2] == '1' ) {
count++;
i += 3;
} else
{
i++;
}
}
return count;
}
var s = "10101010101" ;
var n = s.length;
document.write(countSubStr(s, n));
</script>
|
Time Complexity: O(n), where n is the length of the string.
Auxiliary Space: O(1) as constant extra space is used
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...