Check if all substrings of length K of a Binary String has equal count of 0s and 1s
Last Updated :
17 May, 2021
Given a binary string S of length N and an even integer K, the task is to check if all substrings of length K contains an equal number of 0s and 1s. If found to be true, print “Yes”. Otherwise, print “No”.
Examples:
Input: S = "101010", K = 2
Output: Yes
Explanation:
Since all the substrings of length 2 has equal number of 0s and 1s, the answer is Yes.
Input: S = "101011", K = 4
Output: No
Explanation:
Since substring "1011" has unequal count of 0s and 1s, the answer is No..
Naive Approach: The simplest approach to solve the problem is to generate all substrings of length K and check it if it contains an equal count of 1s and 0s or not.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The main observation for optimizing the above approach is, for the string S to have an equal count of 0 and 1 in substrings of length K, S[i] must be equal to S[i + k]. Follow the steps below to solve the problem:
- Traverse the string and for every ith character, check if S[i] = S[j] where (i == (j % k))
- If found to be false at any instant, then print "No".
- Otherwise, check the first substring of length K and if it contains an equal count of 1s and 0s or not. If found to be true, then print "Yes". Otherwise, print "No".
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <iostream>
using namespace std;
// Function to check if the substring
// of length K has equal 0 and 1
int check(string& s, int k)
{
int n = s.size();
// Traverse the string
for (int i = 0; i < k; i++) {
for (int j = i; j < n; j += k) {
// Check if every K-th character
// is the same or not
if (s[i] != s[j])
return false;
}
}
int c = 0;
// Traverse substring of length K
for (int i = 0; i < k; i++) {
// If current character is 0
if (s[i] == '0')
// Increment count
c++;
// Otherwise
else
// Decrement count
c--;
}
// Check for equal 0s and 1s
if (c == 0)
return true;
else
return false;
}
// Driver code
int main()
{
string s = "101010";
int k = 2;
if (check(s, k))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
Java
// Java program for
// the above approach
import java.util.*;
class GFG{
// Function to check if the substring
// of length K has equal 0 and 1
static boolean check(String s, int k)
{
int n = s.length();
// Traverse the String
for (int i = 0; i < k; i++)
{
for (int j = i; j < n; j += k)
{
// Check if every K-th character
// is the same or not
if (s.charAt(i) != s.charAt(j))
return false;
}
}
int c = 0;
// Traverse subString of length K
for (int i = 0; i < k; i++)
{
// If current character is 0
if (s.charAt(i) == '0')
// Increment count
c++;
// Otherwise
else
// Decrement count
c--;
}
// Check for equal 0s and 1s
if (c == 0)
return true;
else
return false;
}
// Driver code
public static void main(String[] args)
{
String s = "101010";
int k = 2;
if (check(s, k))
System.out.print("Yes" + "\n");
else
System.out.print("No" + "\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function to check if the substring
# of length K has equal 0 and 1
def check(s, k):
n = len(s)
# Traverse the string
for i in range(k):
for j in range(i, n, k):
# Check if every K-th character
# is the same or not
if (s[i] != s[j]):
return False
c = 0
# Traverse substring of length K
for i in range(k):
# If current character is 0
if (s[i] == '0'):
# Increment count
c += 1
# Otherwise
else:
# Decrement count
c -= 1
# Check for equal 0s and 1s
if (c == 0):
return True
else:
return False
# Driver code
s = "101010"
k = 2
if (check(s, k) != 0):
print("Yes")
else:
print("No")
# This code is contributed by sanjoy_62
C#
// C# program for
// the above approach
using System;
class GFG{
// Function to check if the substring
// of length K has equal 0 and 1
static bool check(String s, int k)
{
int n = s.Length;
// Traverse the String
for (int i = 0; i < k; i++)
{
for (int j = i; j < n; j += k)
{
// Check if every K-th character
// is the same or not
if (s[i] != s[j])
return false;
}
}
int c = 0;
// Traverse subString of length K
for (int i = 0; i < k; i++)
{
// If current character is 0
if (s[i] == '0')
// Increment count
c++;
// Otherwise
else
// Decrement count
c--;
}
// Check for equal 0s and 1s
if (c == 0)
return true;
else
return false;
}
// Driver code
public static void Main(String[] args)
{
String s = "101010";
int k = 2;
if (check(s, k))
Console.Write("Yes" + "\n");
else
Console.Write("No" + "\n");
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// javascript program for the
// above approach
// Function to check if the substring
// of length K has equal 0 and 1
function check(s, k)
{
let n = s.length;
// Traverse the String
for (let i = 0; i < k; i++)
{
for (let j = i; j < n; j += k)
{
// Check if every K-th character
// is the same or not
if (s[i] != s[j])
return false;
}
}
let c = 0;
// Traverse subString of length K
for (let i = 0; i < k; i++)
{
// If current character is 0
if (s[i]== '0')
// Increment count
c++;
// Otherwise
else
// Decrement count
c--;
}
// Check for equal 0s and 1s
if (c == 0)
return true;
else
return false;
}
// Driver Code
let s = "101010";
let k = 2;
if (check(s, k))
document.write("Yes" + "<br/>");
else
document.write("No");
// This code is contributed by target_2.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Count of binary strings of length N having equal count of 0's and 1's Given an integer N, the task is to find the number of binary strings possible of length N having same frequency of 0s and 1s. If such string is possible of length N, print -1. Note: Since the count can be very large, return the answer modulo 109+7.Examples: Input: N = 2 Output: 2 Explanation: All po
6 min read
Generate all Binary Strings of length N with equal count of 0s and 1s Given an integer N, the task is to generate all the binary strings with equal 0s and 1s. If no strings are possible, print -1 Examples: Input: N = 2 Output: â01â, â10âExplanation: All possible binary strings of length 2 are: 01, 10, 11, 00. Out of these, only 2 have equal number of 0s and 1s Input:
6 min read
Count of setbits in bitwise OR of all K length substrings of given Binary String Given a binary string str of length N, the task is to find the number of setbits in the bitwise OR of all the K length substrings of string str. Examples: Input: N = 4, K = 3, str = "1111"Output: 3Explanation: All 3-sized substrings of S are:"111" and "111". The OR of these strings is "111". Therefo
8 min read
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
Check if a binary string contains all permutations of length k Given a binary string and k, to check whether it's contains all permutations of length k or not. Examples: Input : Binary string 11001 k : 2 Output : Yes 11001 contains all possibilities of binary sequences with k = 2, 00, 01, 10, 11 Input : Binary string: 1001 k : 2 Output: No 1001 does not contain
13 min read
Longest balanced binary substring with equal count of 1s and 0s Given a binary string str[] of size N. The task is to find the longest balanced substring. A substring is balanced if it contains an equal number of 0 and 1. Examples: Input: str = "110101010"Output: 10101010Explanation: The formed substring contain equal count of 1 and 0 i.e, count of 1 and 0 is sa
8 min read
Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring Given an integer N, the task is to find the number of possible binary strings of length N with an equal frequency of 0's and 1's in which frequency of 1's are greater or equal to the frequency of 0's in every prefix substring. Examples: Input: N = 2Output: 1Explanation:All possible binary strings of
7 min read
Count of substrings of a given Binary string with all characters same 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: 4Explanation: Three sub-strings are "1", "1", "11" which have only 1 in them, and one substring is there
10 min read
Count of substrings of a Binary string containing only 1s Given a binary string of length N, we need to find out how many substrings of this string contain only 1s. Examples: Input: S = "0110111"Output: 9Explanation:There are 9 substring with only 1's characters. "1" comes 5 times. "11" comes 3 times. "111" comes 1 time. Input: S = "000"Output: 0 The_Appro
6 min read
Check if a binary string has a 0 between 1s or not | Set 1 (General approach) Given a string of 0 and 1, we need to check that the given string is valid or not. The given string is valid when there is no zero present in between 1's. For example, 1111, 0000111110, 1111000 are valid strings but 01010011, 01010, 101 are not. One approach to solving the problem is discussed here,
11 min read