Maximum count of unique index 10 or 01 substrings in given Binary string
Last Updated :
09 Feb, 2022
Given a binary string str of length N, the task is to count the maximum number of adjacent pairs of form “01” or “10” that can be formed from the given binary string when one character can be considered for only one pair.
Note: Adjacent pair means pair formed using adjacent characters.
Examples:
Input: str = “0101110”
Output: 3
Explanation: The three pairs are “01” at the starting first,
“01” starting at 2nd index (0 based indexing)
and “10” at the end of the string.
Notice 2 pairs can also be formed by using “10” from index 1 and “10” at last.
But that does not give the maximum number of adjacent pairs.
Input: str = “0011”
Output: 1
Input: str = “11”
Output: 0
Approach: This is a implementation based problem. Follow the steps mentioned here to solve the problem:
- Traverse string from left to right.
- Initialize count of pairs with 0 and consider the previous character as free.
- Run a loop from 1 to size of the string.
- Check if the previous character is opposite to the current character or not and also if it is free or not
- If yes then increment count of pairs and set the character as not free.
- Else continue the traversal in the loop considering the character as free.
- Print the count of pairs.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void check_pairs(string str)
{
int pairs = 0;
bool prev_c = true ;
for ( int i = 1; i < str.size(); i++) {
if (str[i] != str[i - 1] && prev_c) {
prev_c = false ;
pairs++;
}
else {
prev_c = true ;
}
}
cout << pairs;
}
int main()
{
string str = "0101110" ;
check_pairs(str);
return 0;
}
|
Java
class GFG
{
static void check_pairs(String str)
{
int pairs = 0 ;
boolean prev_c = true ;
for ( int i = 1 ; i < str.length(); i++)
{
if (str.charAt(i) != str.charAt(i - 1 ) && prev_c) {
prev_c = false ;
pairs++;
}
else {
prev_c = true ;
}
}
System.out.println(pairs);
}
public static void main(String args[])
{
String str = "0101110" ;
check_pairs(str);
}
}
|
Python3
def check_pairs( str ):
pairs = 0
prev_c = True
for i in range ( 1 , len ( str )):
if ( str [i] ! = str [i - 1 ] and prev_c):
prev_c = False
pairs + = 1
else :
prev_c = True
print (pairs)
if __name__ = = "__main__" :
str = "0101110"
check_pairs( str )
|
C#
using System;
class GFG
{
static void check_pairs( string str)
{
int pairs = 0;
bool prev_c = true ;
for ( int i = 1; i < str.Length; i++)
{
if (str[i] != str[i - 1] && prev_c) {
prev_c = false ;
pairs++;
}
else {
prev_c = true ;
}
}
Console.Write(pairs);
}
public static int Main()
{
string str = "0101110" ;
check_pairs(str);
return 0;
}
}
|
Javascript
<script>
function check_pairs(str)
{
let pairs = 0;
let prev_c = true ;
for (let i = 1; i < str.length; i++)
{
if (str[i] != str[i - 1] && prev_c)
{
prev_c = false ;
pairs++;
}
else {
prev_c = true ;
}
}
document.write(pairs);
}
let str = "0101110" ;
check_pairs(str);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...