给定长度为N的二进制字符串S ,任务是最大程度地减少从给定的二进制字符串S中重复删除0和1的交替子序列的次数,以使字符串空。
例子:
Input: S = “0100100111”
Output: 3
Explanation:
Remove subsequence “010101” from S to modify it to “0011”.
Remove “01” from “0011” to make it “01”.
Finally, remove “01” to make it an empty string.
Input: S = “1111”
Output: 4
方法:可以通过观察要删除的0和1的交替子序列来解决给定的问题,并且要删除所有连续的字符1s或0s只能在每个单独的操作中删除,而不能在单个操作中删除。
因此,所需的最少操作数是连续0和1s的最大计数。
下面是上述方法的实现:
C++
#include
using namespace std;
void minOpsToEmptyString(string S, int N)
{
// Initialize variables
int one = 0, zero = 0;
int x0 = 0, x1 = 0;
// Traverse the string
for (int i = 0; i < N; i++) {
// If current character is 0
if (S[i] == '0') {
x0++;
x1 = 0;
}
else {
x1++;
x0 = 0;
}
// Update maximum consecutive
// 0s and 1s
zero = max(x0, zero);
one = max(x1, one);
}
// Print the minimum operation
cout << max(one, zero) << endl;
}
// Driver code+
int main()
{
// input string
string S = "0100100111";
// length of string
int N = S.length();
// Function Call
minOpsToEmptyString(S, N);
}
// This code is contributed by aditya7409
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to find the minimum
// number of operations required
// to empty the string
public static void
minOpsToEmptyString(String S,
int N)
{
// Initialize variables
int one = 0, zero = 0;
int x0 = 0, x1 = 0;
// Traverse the string
for (int i = 0; i < N; i++) {
// If current character is 0
if (S.charAt(i) == '0') {
x0++;
x1 = 0;
}
else {
x1++;
x0 = 0;
}
// Update maximum consecutive
// 0s and 1s
zero = Math.max(x0, zero);
one = Math.max(x1, one);
}
// Print the minimum operation
System.out.println(
Math.max(one, zero));
}
// Driver Code
public static void main(String[] args)
{
String S = "0100100111";
int N = S.length();
// Function Call
minOpsToEmptyString(S, N);
}
}
Python3
# Python3 program for the above approach
def minOpsToEmptyString(S, N):
# Initialize variables
one = 0
zero = 0
x0 = 0
x1 = 0
# Traverse the string
for i in range(N):
# If current character is 0
if (S[i] == '0'):
x0 += 1
x1 = 0
else:
x1 += 1
x0 = 0
# Update maximum consecutive
# 0s and 1s
zero = max(x0, zero)
one = max(x1, one)
# Print the minimum operation
print(max(one, zero))
# Driver code+
if __name__ == "__main__":
# input string
S = "0100100111"
# length of string
N = len(S)
# Function Call
minOpsToEmptyString(S, N)
# This code is contributed by chitranayal
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the minimum
// number of operations required
// to empty the string
public static void
minOpsToEmptyString(string S, int N)
{
// Initialize variables
int one = 0, zero = 0;
int x0 = 0, x1 = 0;
// Traverse the string
for (int i = 0; i < N; i++)
{
// If current character is 0
if (S[i] == '0')
{
x0++;
x1 = 0;
}
else
{
x1++;
x0 = 0;
}
// Update maximum consecutive
// 0s and 1s
zero = Math.Max(x0, zero);
one = Math.Max(x1, one);
}
// Print the minimum operation
Console.WriteLine(Math.Max(one, zero));
}
// Driver Code
static public void Main()
{
string S = "0100100111";
int N = S.Length;
// Function Call
minOpsToEmptyString(S, N);
}
}
// This code is contributed by Dharanendra L V
输出:
3
时间复杂度: O(N)
辅助空间: O(N)