📌  相关文章
📜  检查二进制字符串的所有长度为K的子字符串是否具有等于0和1的计数

📅  最后修改于: 2021-04-21 23:49:59             🧑  作者: Mango

给定一个长度为N且二进制数为偶数K的二进制字符串S ,任务是检查所有长度为K的子字符串是否都包含相等的0 s和1 s。如果发现是真的,则打印“是”。否则,打印“否”。

例子:

天真的方法:解决该问题的最简单方法是生成所有长度为K的子字符串,并检查它是否包含等于1和0的相等计数。

时间复杂度: O(N 2 )
辅助空间: O(1)

高效的方法:优化上述方法的主要观察结果是,对于字符串S在长度为K的子字符串中具有等于0和1的相等计数, S [i]必须等于S [i + k] 。请按照以下步骤解决问题:

  • 遍历字符串并对于每个i字符,检查S [i] = S [j]其中(i ==(j%k))
  • 如果在任何时候发现错误,则打印“否”。
  • 否则,请检查长度为K的第一个子字符串,以及是否包含等于1s和0s的相等计数。如果发现是真的,则打印“是”。否则,打印“否”。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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


输出:
Yes







时间复杂度: O(N)
辅助空间: O(1)