使二进制字符串的所有字符都等于0需要重复翻转的子字符串的最大长度
给定一个二进制字符串S ,任务是找到重复翻转以使二进制字符串的所有字符等于'0'所需的子字符串的最大长度。
例子:
Input: S = “010”
Output: 2
Explanation:
Following are the order of flipping of substring of at least K for the value of K as 2:
- Flip the substring S[0, 2] of length 3(>= K) modifies the string S to “101”.
- Flip the substring S[0, 1] of length 2(>= K) modifies the string S to “011”.
- Flip the substring S[1, 2] of length 2(>= K) modifies the string S to “000”.
For the value of K as 2(which is the maximum possible value) all the characters of the string can be made 0. Therefore, print 2.
Input: S = “00001111”
Output: 4
方法:给定的问题可以通过遍历给定的字符串S来解决,现在如果在任何点相邻的字符不相同,则翻转一个子字符串LHS或RHS 。为此,取LHS和RHS的最大长度。可以有多个相邻的字符不相等的地方。对于每对子字符串,所需的最大值将不同。现在将所有字符更改为“0”,取所有最大值中的最小值。请按照以下步骤解决问题:
- 初始化变量,比如ans作为N存储K的最大可能值。
- 使用变量i迭代范围[0, N – 1)并执行以下步骤:
- 如果s[i]和s[i + 1]的值不相同,则将ans的值更新为(i + 1)或(N – i – 1)的ans的最小值或最大值。
- 执行上述步骤后,打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum value of K
// such that flipping substrings of size
// at least K make all characters 0s
int maximumK(string& S)
{
int N = S.length();
// Stores the maximum value of K
int ans = N;
int flag = 0;
// Traverse the given string S
for (int i = 0; i < N - 1; i++) {
// Store the minimum of the
// maximum of LHS and RHS length
if (S[i] != S[i + 1]) {
// Flip performed
flag = 1;
ans = min(ans, max(i + 1,
N - i - 1));
}
}
// If no flips performed
if (flag == 0)
return 0;
// Return the possible value of K
return ans;
}
// Driver Code
int main()
{
string S = "010";
cout << maximumK(S);
return 0;
}
Java
// Java code for above approach
import java.util.*;
class GFG{
// Function to find the maximum value of K
// such that flipping substrings of size
// at least K make all characters 0s
static int maximumK(String S)
{
int N = S.length();
// Stores the maximum value of K
int ans = N;
int flag = 0;
// Traverse the given string S
for (int i = 0; i < N - 1; i++) {
// Store the minimum of the
// maximum of LHS and RHS length
if (S.charAt(i) != S.charAt(i + 1)) {
// Flip performed
flag = 1;
ans = Math.min(ans, Math.max(i + 1,
N - i - 1));
}
}
// If no flips performed
if (flag == 0)
return 0;
// Return the possible value of K
return ans;
}
// Driver Code
public static void main(String[] args)
{
String S = "010";
System.out.print(maximumK(S));
}
}
// This code is contributed by target_2.
Python3
# Python 3 program for the above approach
# Function to find the maximum value of K
# such that flipping substrings of size
# at least K make all characters 0s
def maximumK(S):
N = len(S)
# Stores the maximum value of K
ans = N
flag = 0
# Traverse the given string S
for i in range(N - 1):
# Store the minimum of the
# maximum of LHS and RHS length
if (S[i] != S[i + 1]):
# Flip performed
flag = 1
ans = min(ans, max(i + 1,N - i - 1))
# If no flips performed
if (flag == 0):
return 0
# Return the possible value of K
return ans
# Driver Code
if __name__ == '__main__':
S = "010"
print(maximumK(S))
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# code for the above approach
using System;
public class GFG {
// Function to find the maximum value of K
// such that flipping substrings of size
// at least K make all characters 0s
static int maximumK(String S)
{
int N = S.Length;
// Stores the maximum value of K
int ans = N;
int flag = 0;
// Traverse the given string S
for (int i = 0; i < N - 1; i++) {
// Store the minimum of the
// maximum of LHS and RHS length
if (S[i] != S[i + 1]) {
// Flip performed
flag = 1;
ans = Math.Min(ans,
Math.Max(i + 1, N - i - 1));
}
}
// If no flips performed
if (flag == 0)
return 0;
// Return the possible value of K
return ans;
}
// Driver Code
static public void Main()
{
// Code
string S = "010";
Console.Write(maximumK(S));
}
}
// This code is contributed by Potta Lokesh
Javascript
输出:
2
时间复杂度: O(N)
辅助空间: O(1)