给定长度为N的二进制字符串S和偶数整数K ,任务是检查长度为K 的所有子字符串是否包含相同数量的0和1 。如果发现是真的,打印“是”。否则,打印“否”。
例子:
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..
朴素方法:解决问题的最简单方法是生成长度为 K 的所有子串,并检查它是否包含相等的 1 和 0 计数。
时间复杂度: O(N 2 )
辅助空间: O(1)
有效的方法:优化上述方法的主要观察结果是,对于字符串S 在长度为K 的子字符串中具有相等的 0 和 1 计数, S[i]必须等于S[i + k] 。请按照以下步骤解决问题:
- 遍历字符串,对于每个第i个字符,检查是否S[i] = S[j] where (i == (j % k))
- 如果在任何时候发现是假的,则打印“否”。
- 否则,检查长度为 K 的第一个子串,以及它是否包含相等的 1 和 0 计数。如果发现是真的,则打印“是”。否则,打印“否”。
下面是上述方法的实现:
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
Javascript
输出:
Yes
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。