可以从给定的二进制字符串中删除的“010..”子序列的最大计数
给定一个大小为N的二进制字符串S ,任务是找到可以从给定字符串S中删除的长度至少为2的“010..”形式的二进制子序列的最大数量。
例子:
Input: S = “110011010”
Output: 3
Explanation:
Following are the subsequence removed:
Operation 1: Choose the subsequence as “01” of indices {2, 4}, and deleting it modifies the string S = “1101010”.
Operation 2: Choose the subsequence as “01” of indices {2, 3}, and deleting it modifies the string S = “11010”.
Operation 3: Choose the subsequence as “01” of indices {2, 3}, and deleting it modifies the string S = “110”.
From the above observations, the maximum number of times subsequence is removed is 3.
Input: S = “00111110011”
Output: 4
方法:给定的问题可以通过每次删除类型为“01”的子序列来解决,以最大化删除的子序列数。因此,这可以通过保持一个存储字符数计数的变量0来维持。请按照以下步骤解决问题:
- 初始化一个变量,例如将cnt设置为0以存储字符串中出现的0的数量。
- 初始化变量,比如ans为0以计算执行的删除操作的总数。
- 使用变量i遍历字符串S并执行以下步骤:
- 如果S[i]的值为X则将cnt的值增加1 。
- 否则,如果cnt的值大于0 ,则减少cnt的值并将ans的值增加 1。
- 完成上述步骤后,打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the maximum number
// of operations performed on the string
void countOperations(string S)
{
// Size of the string
int n = S.length();
// Stores the maximum number of
// operations performed
int ans = 0;
// Stores the count of 'X' occurred
// so far
int cnt = 0;
// Traverse the string
for (int i = 0; i < n; i++) {
// Check if current char
// is 'X'
if (S[i] == '0') {
cnt++;
}
else {
// Check if the value of
// cnt is greater than 0
if (cnt > 0) {
// Decrement the value
// of cnt
cnt--;
// Increment the value
// of ans
ans++;
}
}
}
// Print the value of ans
cout << ans;
}
// Driver Code
int main()
{
string S = "110011010";
countOperations(S);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to count the maximum number
// of operations performed on the string
public static void countOperations(String S)
{
// Size of the string
int n = S.length();
// Stores the maximum number of
// operations performed
int ans = 0;
// Stores the count of 'X' occurred
// so far
int cnt = 0;
// Traverse the string
for (int i = 0; i < n; i++) {
// Check if current char
// is 'X'
if (S.charAt(i) == '0') {
cnt++;
}
else {
// Check if the value of
// cnt is greater than 0
if (cnt > 0) {
// Decrement the value
// of cnt
cnt--;
// Increment the value
// of ans
ans++;
}
}
}
// Print the value of ans
System.out.println(ans);
}
// Driver code
public static void main (String[] args)
{
String S = "110011010";
countOperations(S);
}
}
// This code is contributed by Manu Pathria
Python3
# Python program for the above approach
# Function to count the maximum number
# of operations performed on the string
def countOperations(S):
# Size of the string
n = len(S)
# Stores the maximum number of
# operations performed
ans = 0
# Stores the count of 'X' occurred
# so far
cnt = 0
# Traverse the string
for i in range(n):
# Check if current char
# is 'X'
if (S[i] == '0'):
cnt+=1
else:
# Check if the value of
# cnt is greater than 0
if (cnt > 0):
# Decrement the value
# of cnt
cnt-=1
# Increment the value
# of ans
ans+=1
# Print value of ans
print (ans)
# Driver Code
if __name__ == '__main__':
S = "110011010"
countOperations(S)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to count the maximum number
// of operations performed on the string
public static void countOperations(string S)
{
// Size of the string
int n = S.Length;
// Stores the maximum number of
// operations performed
int ans = 0;
// Stores the count of 'X' occurred
// so far
int cnt = 0;
// Traverse the string
for (int i = 0; i < n; i++) {
// Check if current char
// is 'X'
if (S[i] == '0') {
cnt++;
}
else {
// Check if the value of
// cnt is greater than 0
if (cnt > 0) {
// Decrement the value
// of cnt
cnt--;
// Increment the value
// of ans
ans++;
}
}
}
// Print the value of ans
Console.Write(ans);
}
// Driver code
public static void Main (string[] args)
{
string S = "110011010";
countOperations(S);
}
}
// This code is contributed by ukasp.
Javascript
3
时间复杂度: O(N)
辅助空间: O(1)