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 **0**s and **1**s. If found to be true, print “Yes”. Otherwise, print “No”.

**Examples:**

Input:S = “101010”, K = 2Output:YesExplanation:

Since all the substrings of length 2 has equal number of 0s and 1s, the answer is Yes.

Input:S = “101011”, K = 4Output:NoExplanation:

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(N^{2})**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 i
^{th}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;` `}` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Time Complexity: **O(N)**Auxiliary Space: **O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count ways to split a Binary String into three substrings having equal count of zeros
- 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
- Count of Substrings having Sum equal to their Length
- Check if binary representations of 0 to N are present as substrings in given binary string
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Split a given string into substrings of length K with equal sum of ASCII values
- Minimize cost to convert given string into concatenation of equal substrings of length K
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Split the binary string into substrings with equal number of 0s and 1s
- Count of substrings of a given Binary string with all characters same
- Check if a string can be split into even length palindromic substrings
- Count all Prime Length Palindromic Substrings
- Count of binary strings of length N having equal count of 0's and 1's
- Given a binary string, count number of substrings that start and end with 1.
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Count of substrings of a binary string containing K ones
- Count of substrings of a Binary string containing only 1s
- Count Substrings with equal number of 0s, 1s and 2s
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.