在最多翻转一个“1”后,最大化给定二进制字符串中长度为 X 的 0 子数组
给定一个长度为N的二进制字符串str和一个正整数X ,任务是通过最多翻转一个1来最大化仅由0组成的X长度子数组的计数。一位将仅在一个子阵列中考虑。
例子:
Input: str = “0010001″, X = 2
Output: 3
Explanation: Flip str[2] from 1 to 0, -> str = “0000001” -> Now count of X length subarrays of 0 = 3
Flipping index 6 will also give resultant count as 3.
Input: str = “00100”, X = 2
Output: 2
Explanation: Flipping str[2] from 1 to 0 will not change the maximum group of 0’s.It will remain same as 2 only.
方法:要计算包含 X 个零的最大零组,请遵循以下给定步骤:
- 查找包含 X 个连续零的零组的数量。
- 迭代给定的字符串,同时如果在迭代过程中出现“1”,则保持左零和右零的计数。
- 现在检查我们是否翻转当前的“1”是否会影响零组或否,如果它会将该组增加 1 并返回它,否则继续迭代并一次又一次地执行上述步骤。
- 返回组数。
下面是上述方法的实现:
C++
// C++ program of finding
// the maximum group of zeroes
// in a binary string after
// flipping at most one '1'
#include
using namespace std;
// Function to find the maximum
// number of groups of X
// consecutive Zeroes
int maxGroupOfZeroes(string str, int X)
{
int n = str.size();
// Number of groups before flipping '1'
int groups = 0;
// Iterating over the string
int i = 0;
while (i < n) {
// It will keep the track
// of consecutive zeroes
int zero = 0;
while (i < n && str[i] == '0') {
zero++;
i++;
}
i++;
// It will break those
// consecutive zeroes into groups
groups += (zero / X);
}
// Left variable will count
// the consecutive zeroes
// on the left side of '1'
int left = 0;
// Right variable will count
// the consecutive zeroes
// on the right size of '1'
int right = 0;
// Iterating str from 0 index
i = 0;
while (i < n) {
char ch = str[i];
// If it is '0' keep
// incrementing left zeroes
if (ch == '0') {
left++;
i++;
}
// Else increment
// right zeroes while
// character is '0'
else {
int j = i;
i++;
while (i < n && str[i] != '1') {
i++;
right++;
}
int contribute = (left + right + 1) / X;
int without = ((left / X) + (right / X));
// Checking after flipping
// current '1' will
// it affect the groups or not
if (without != contribute) {
return ++groups;
}
// If it doesn't affect
// then right zeroes
// will become left zeroes
// and for right
// zeroes we will have
// to count it again
left = right;
right = 0;
}
}
// Return number of groups
return groups;
}
// Driver Code
int main()
{
string str = "0010001";
int X = 2;
int max_groups = maxGroupOfZeroes(str, X);
cout << (max_groups);
return 0;
}
// This code is contributed by rakeshsahni
Java
// Java program of finding
// the maximum group of zeroes
// in a binary string after
// flipping at most one '1'
import java.util.*;
class GFG {
// Function to find the maximum
// number of groups of X
// consecutive Zeroes
public static int maxGroupOfZeroes(
String str, int X)
{
int n = str.length();
// Number of groups before flipping '1'
int groups = 0;
// Iterating over the string
int i = 0;
while (i < n) {
// It will keep the track
// of consecutive zeroes
int zero = 0;
while (i < n
&& str.charAt(i) == '0') {
zero++;
i++;
}
i++;
// It will break those
// consecutive zeroes into groups
groups += (zero / X);
}
// Left variable will count
// the consecutive zeroes
// on the left side of '1'
int left = 0;
// Right variable will count
// the consecutive zeroes
// on the right size of '1'
int right = 0;
// Iterating str from 0 index
i = 0;
while (i < n) {
char ch = str.charAt(i);
// If it is '0' keep
// incrementing left zeroes
if (ch == '0') {
left++;
i++;
}
// Else increment
// right zeroes while
// character is '0'
else {
int j = i;
i++;
while (i < n
&& str.charAt(i) != '1') {
i++;
right++;
}
int contribute
= (left + right + 1) / X;
int without
= ((left / X) + (right / X));
// Checking after flipping
// current '1' will
// it affect the groups or not
if (without != contribute) {
return ++groups;
}
// If it doesn't affect
// then right zeroes
// will become left zeroes
// and for right
// zeroes we will have
// to count it again
left = right;
right = 0;
}
}
// Return number of groups
return groups;
}
// Driver Code
public static void main(String[] args)
{
String str = "0010001";
int X = 2;
int max_groups
= maxGroupOfZeroes(str, X);
System.out.println(max_groups);
}
}
Python
# Python program of finding
# the maximum group of zeroes
# in a binary string after
# flipping at most one '1'
# Function to find the maximum
# number of groups of X
# consecutive Zeroes
def maxGroupOfZeroes(str, X):
n = len(str)
# Number of groups before flipping '1'
groups = 0
# Iterating over the string
i = 0
while (i < n):
# It will keep the track
# of consecutive zeroes
zero = 0
while (i < n and str[i] == '0'):
zero += 1
i += 1
i += 1
# It will break those
# consecutive zeroes into groups
groups += (zero // X)
# Left variable will count
# the consecutive zeroes
# on the left side of '1'
left = 0
# Right variable will count
# the consecutive zeroes
# on the right size of '1'
right = 0
# Iterating str from 0 index
j = 0
while (j < n):
ch = str[j]
# If it is '0' keep
# incrementing left zeroes
if (ch == '0'):
left += 1
j += 1
# Else increment
# right zeroes while
# character is '0'
else:
k = j
j += 1
while (j < n and str[j] != '1'):
j += 1
right += 1
contribute = (left + right + 1) // X
without = ((left // X) + (right // X))
# Checking after flipping
# current '1' will
# it affect the groups or not
if (without != contribute):
return 1 + groups
# If it doesn't affect
# then right zeroes
# will become left zeroes
# and for right
# zeroes we will have
# to count it again
left = right
right = 0
# Return number of groups
return groups
# Driver Code
str = "0010001"
X = 2
max_groups = maxGroupOfZeroes(str, X)
print(max_groups)
# This code is contributed by Samim Hossain Mondal.
C#
// C# program of finding
// the maximum group of zeroes
// in a binary string after
// flipping at most one '1'
using System;
class GFG {
// Function to find the maximum
// number of groups of X
// consecutive Zeroes
public static int maxGroupOfZeroes(
String str, int X)
{
int n = str.Length;
// Number of groups before flipping '1'
int groups = 0;
// Iterating over the string
int i = 0;
while (i < n) {
// It will keep the track
// of consecutive zeroes
int zero = 0;
while (i < n
&& str[i] == '0') {
zero++;
i++;
}
i++;
// It will break those
// consecutive zeroes into groups
groups += (zero / X);
}
// Left variable will count
// the consecutive zeroes
// on the left side of '1'
int left = 0;
// Right variable will count
// the consecutive zeroes
// on the right size of '1'
int right = 0;
// Iterating str from 0 index
i = 0;
while (i < n) {
char ch = str[i];
// If it is '0' keep
// incrementing left zeroes
if (ch == '0') {
left++;
i++;
}
// Else increment
// right zeroes while
// character is '0'
else {
int j = i;
i++;
while (i < n
&& str[i] != '1') {
i++;
right++;
}
int contribute
= (left + right + 1) / X;
int without
= ((left / X) + (right / X));
// Checking after flipping
// current '1' will
// it affect the groups or not
if (without != contribute) {
return ++groups;
}
// If it doesn't affect
// then right zeroes
// will become left zeroes
// and for right
// zeroes we will have
// to count it again
left = right;
right = 0;
}
}
// Return number of groups
return groups;
}
// Driver Code
public static void Main()
{
String str = "0010001";
int X = 2;
int max_groups
= maxGroupOfZeroes(str, X);
Console.Write(max_groups);
}
}
// This code is contributed by saurabh_jaiswal.
Javascript
输出
3
时间复杂度: O(N)
辅助空间: O(1)