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++
// C++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;
// Count pairs function
void check_pairs(string str)
{
// Initialize pairs with 0
int pairs = 0;
// Previous char is free to pair
bool prev_c = true;
// Traverse string from second position
for (int i = 1; i < str.size(); i++) {
// Check both char are opposite or not
// and also check previous char
// is free or not
if (str[i] != str[i - 1] && prev_c) {
// Once previous char paired
// with other make it false
prev_c = false;
// Increment pairs count
pairs++;
}
else {
// Previous char is free for pair
prev_c = true;
}
}
// Print count of pairs of two characters
cout << pairs;
}
// Driver Code
int main()
{
string str = "0101110";
// Function call
check_pairs(str);
return 0;
}
Java
// Java code to implement the above approach
class GFG
{
// Count pairs function
static void check_pairs(String str)
{
// Initialize pairs with 0
int pairs = 0;
// Previous char is free to pair
boolean prev_c = true;
// Traverse String from second position
for (int i = 1; i < str.length(); i++)
{
// Check both char are opposite or not
// and also check previous char
// is free or not
if (str.charAt(i) != str.charAt(i - 1) && prev_c) {
// Once previous char paired
// with other make it false
prev_c = false;
// Increment pairs count
pairs++;
}
else {
// Previous char is free for pair
prev_c = true;
}
}
// Print count of pairs of two characters
System.out.println(pairs);
}
// Driver Code
public static void main(String args[])
{
String str = "0101110";
// Function call
check_pairs(str);
}
}
// This code is contributed by gfgking
Python3
# python3 code to implement the above approach
# Count pairs function
def check_pairs(str):
# Initialize pairs with 0
pairs = 0
# Previous char is free to pair
prev_c = True
# Traverse string from second position
for i in range(1, len(str)):
# Check both char are opposite or not
# and also check previous char
# is free or not
if (str[i] != str[i - 1] and prev_c):
# Once previous char paired
# with other make it false
prev_c = False
# Increment pairs count
pairs += 1
else:
# Previous char is free for pair
prev_c = True
# Print count of pairs of two characters
print(pairs)
# Driver Code
if __name__ == "__main__":
str = "0101110"
# Function call
check_pairs(str)
# This code is contributed by rakeshsahni
C#
// C# code to implement the above approach
using System;
class GFG
{
// Count pairs function
static void check_pairs(string str)
{
// Initialize pairs with 0
int pairs = 0;
// Previous char is free to pair
bool prev_c = true;
// Traverse string from second position
for (int i = 1; i < str.Length; i++)
{
// Check both char are opposite or not
// and also check previous char
// is free or not
if (str[i] != str[i - 1] && prev_c) {
// Once previous char paired
// with other make it false
prev_c = false;
// Increment pairs count
pairs++;
}
else {
// Previous char is free for pair
prev_c = true;
}
}
// Print count of pairs of two characters
Console.Write(pairs);
}
// Driver Code
public static int Main()
{
string str = "0101110";
// Function call
check_pairs(str);
return 0;
}
}
// This code is contributed by Taranpreet
JavaScript
<script>
// JavaScript code for the above approach
// Count pairs function
function check_pairs(str)
{
// Initialize pairs with 0
let pairs = 0;
// Previous char is free to pair
let prev_c = true;
// Traverse string from second position
for (let i = 1; i < str.length; i++)
{
// Check both char are opposite or not
// and also check previous char
// is free or not
if (str[i] != str[i - 1] && prev_c)
{
// Once previous char paired
// with other make it false
prev_c = false;
// Increment pairs count
pairs++;
}
else {
// Previous char is free for pair
prev_c = true;
}
}
// Print count of pairs of two characters
document.write(pairs);
}
// Driver Code
let str = "0101110";
// Function call
check_pairs(str);
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Count of substrings with equal ratios of 0s and 1s till ith index in given Binary String Given a binary string S, the task is to print the maximum number of substrings with equal ratios of 0s and 1s till the ith index from the start. Examples: Input: S = "110001"Output: {1, 2, 1, 1, 1, 2}Explanation: The given string can be partitioned into the following equal substrings: Valid substrin
9 min read
Maximize count of 0s in left and 1s in right substring by splitting given Binary string Given a binary string str, the task is to split the given binary string at any index into two non-empty substrings such that the sum of counts of 0s in the left substring and 1s in the right substring is maximum. Print the sum of such 0s and 1s in the end.Examples: Input: str = "0011110011" Output:
6 min read
Count of non-overlapping sub-strings "101" and "010" in the given binary string 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
5 min read
Count of substrings that start and end with 1 in given Binary String Given a binary string, count the number of substrings that start and end with 1. Examples: Input: "00100101"Output: 3Explanation: three substrings are "1001", "100101" and "101" Input: "1001"Output: 1Explanation: one substring "1001" Recommended PracticeCount SubstringsTry It!Count of substrings tha
12 min read
Maximum bitwise OR one of any two Substrings of given Binary String Given a binary string str, the task is to find the maximum possible OR value of any two substrings of the binary string str. Examples: Input: str = "1110010"Output: "1111110"Explanation: On choosing the substring "1110010" and "11100" we get the OR value as "1111110" which is the maximum value. Inpu
8 min read