清空二进制字符串所需的备用子序列的最小数量
给定一个由N个字符组成的二进制字符串S ,任务是打印从给定字符串S中删除所有字符所需的最小操作数,方法是删除单个字符或删除每个操作中的任何替代字符的子序列。
例子:
Input: S = “010101”
Output: 1
Explanation:
Below are the operations performed:
Operation 1: Consider the subsequence S[0, 5] i.e., “010101” as it contains alternating characters. Therefore, removing this modifies the string to “”.
Hence, the total number of operation required is 1.
Input: S = “00011”
Output: 3
方法:给定的问题可以通过遍历字符串一次并跟踪剩余0和1的最大数量来解决。请按照以下步骤解决问题:
- 初始化一个变量,比如说ans来存储仍然要删除的0和1的最大数量,一个变量cnt0来计算0的数量,以及一个变量cnt1来计算1的数量。
- 从头开始遍历给定的字符串S并执行以下步骤:
- 如果当前字符为0 ,则将cnt0的值加1 ,如果cnt1的值大于0 ,则将其减1 。
- 如果当前字符为1 ,则将cnt1的值增加1 ,如果cnt0的值大于0 ,则将其减少1 。
- 将ans 的值更新为 ans 、 cnt1和cnt0的最大值。
- 完成上述步骤后,打印ans的值作为删除所有字符所需的最少操作次数。
下面是该方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum number
// of operations to empty a binary string
int minOpsToEmptyString(string s)
{
// Stores the resultant number of
// operations
int ans = INT_MIN;
// Stores the number of 0s
int cn0 = 0;
// Stores the number of 1s
int cn1 = 0;
// Traverse the given string
for (int i = 0;
i < s.length(); i++) {
if (s[i] == '0') {
// To balance 0 with 1
// if possible
if (cn1 > 0)
cn1--;
// Increment the value
// of cn0 by 1
cn0++;
}
else {
// To balance 1 with 0
// if possible
if (cn0 > 0)
cn0--;
// Increment the value
// of cn1
cn1++;
}
// Update the maximum number
// of unused 0s and 1s
ans = max({ ans, cn0, cn1 });
}
// Print the resultant count
cout << ans;
}
// Driver Code
int main()
{
string S = "010101";
minOpsToEmptyString(S);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the minimum number
// of operations to empty a binary string
static void minOpsToEmptyString(String s)
{
// Stores the resultant number of
// operations
int ans = Integer.MIN_VALUE;
// Stores the number of 0s
int cn0 = 0;
// Stores the number of 1s
int cn1 = 0;
// Traverse the given string
for(int i = 0; i < s.length(); i++)
{
if (s.charAt(i) == '0')
{
// To balance 0 with 1
// if possible
if (cn1 > 0)
cn1--;
// Increment the value
// of cn0 by 1
cn0++;
}
else
{
// To balance 1 with 0
// if possible
if (cn0 > 0)
cn0--;
// Increment the value
// of cn1
cn1++;
}
// Update the maximum number
// of unused 0s and 1s
ans = Math.max(ans, Math.max(cn0, cn1));
}
// Print the resultant count
System.out.print(ans);
}
// Driver Code
public static void main(String[] args)
{
String S = "010101";
minOpsToEmptyString(S);
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
# Function to find the minimum number
# of operations to empty a binary string
def minOpsToEmptyString(s):
# Stores the resultant number of
# operations
ans = -10**9
# Stores the number of 0s
cn0 = 0
# Stores the number of 1s
cn1 = 0
# Traverse the given string
for i in range(len(s)):
if (s[i] == '0'):
# To balance 0 with 1
# if possible
if (cn1 > 0):
cn1 -= 1
# Increment the value
# of cn0 by 1
cn0 += 1
else:
# To balance 1 with 0
# if possible
if (cn0 > 0):
cn0 -= 1
# Increment the value
# of cn1
cn1 += 1
# Update the maximum number
# of unused 0s and 1s
ans = max([ans, cn0, cn1])
# Print resultant count
print (ans)
# Driver Code
if __name__ == '__main__':
S = "010101"
minOpsToEmptyString(S)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the minimum number
// of operations to empty a binary string
static void minOpsToEmptyString(string s)
{
// Stores the resultant number of
// operations
int ans = 0;
// Stores the number of 0s
int cn0 = 0;
// Stores the number of 1s
int cn1 = 0;
// Traverse the given string
for(int i = 0; i < s.Length; i++)
{
if (s[i] == '0')
{
// To balance 0 with 1
// if possible
if (cn1 > 0)
cn1--;
// Increment the value
// of cn0 by 1
cn0++;
}
else
{
// To balance 1 with 0
// if possible
if (cn0 > 0)
cn0--;
// Increment the value
// of cn1
cn1++;
}
// Update the maximum number
// of unused 0s and 1s
ans = Math.Max(ans, Math.Max(cn0, cn1));
}
// Print the resultant count
Console.Write(ans);
}
// Driver Code
public static void Main()
{
string S = "010101";
minOpsToEmptyString(S);
}
}
// This code is contributed by avijitmondal1998.
Javascript
输出:
1
时间复杂度: O(N)
辅助空间: O(N)