给定大小为N的二进制字符串S ,任务是通过选择任何子字符串“ 01 ”并在一次移动中从中删除任何字符,从而减少字符串的长度,从而找到可以对S执行的最大操作数由1 。
例子:
Input: S = “001111”, N = 6
Output: 5
Explanation:
One way to perform the operations is:
- Select the substring “01” over the range [1, 2] and erase the S[2] ( = ‘1’). The string modifies to “00111”.
- Select the substring “01” over the range [1, 2] and erase the S[2] ( = ‘1’). The string modifies to “0011”.
- Select the substring “01” over the range [1, 2] and erase the S[2] ( = ‘1’). The string modifies to “001”.
- Select the substring “01” over the range [1, 2] and erase the S[1] ( = ‘0’). The string modifies to “01”.
- Select the substring “01” over the range [0, 1] and erase the S[1] ( = ‘1’). The string modifies to “0”.
- Now no characters can be removed.
Therefore, the total number of operations performed is 5, which is the maximum possible.
Input: S=”0101″, N=4
Output: 3
方法:根据以下观察可以解决给定的问题:
- Any 1s present in the prefix of S cannot be removed because there are no 0s before them.
- Any 0s present in the suffix of S cannot be removed because there are no 1s after them.
- All other characters are removable.
- If there are X removable characters, at most X-1 operations can be performed because, eventually, only a single character will remain which cannot be removed.
请按照以下步骤解决问题:
- 初始化两个变量,比如X和Y为0 ,它们分别存储后缀中0的数量和前缀中1的数量,不能删除。
- 迭代字符串S的字符并执行以下步骤:
- 如果当前字符是 ‘ 1′ ,则将Y增加1。
- 否则,停止遍历。
- 迭代字符串S中的字符 逆转 订购并执行以下步骤:
- 如果当前字符为0 ,则将X增加1 。
- 否则,停止遍历。
- 如果X和Y的总和等于N ,则打印0,因为没有可移除字符。
- 否则,打印N-(X+Y)-1作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum moves
// that can be performed on a string
int maxOperations(string S, int N)
{
// Stores 0s in suffix
int X = 0;
// Stores 1s in prefix
int Y = 0;
// Iterate over the characters of
// the string
for (int i = 0; i < N; i++) {
if (S[i] == '0')
break;
Y++;
}
// Iterate until i is greater than
// or equal to 0
for (int i = N - 1; i >= 0; i--) {
if (S[i] == '1')
break;
X++;
}
// If N is equal to x+y
if (N == X + Y)
return 0;
// Return answer
return N - (X + Y) - 1;
}
// Driver code
int main()
{
// Input
string S = "001111";
int N = S.length();
// Function call
cout << maxOperations(S, N) << endl;
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to find the maximum moves
// that can be performed on a string
static int maxOperations(String S, int N)
{
// Stores 0s in suffix
int X = 0;
// Stores 1s in prefix
int Y = 0;
// Iterate over the characters of
// the string
for(int i = 0; i < N; i++)
{
if (S.charAt(i) == '0')
break;
Y++;
}
// Iterate until i is greater than
// or equal to 0
for(int i = N - 1; i >= 0; i--)
{
if (S.charAt(i) == '1')
break;
X++;
}
// If N is equal to x+y
if (N == X + Y)
return 0;
// Return answer
return N - (X + Y) - 1;
}
// Driver code
public static void main(String[] args)
{
// Input
String S = "001111";
int N = S.length();
// Function call
System.out.println(maxOperations(S, N));
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
# Function to find the maximum moves
# that can be performed on a string
def maxOperations(S, N):
# Stores 0s in suffix
X = 0
# Stores 1s in prefix
Y = 0
# Iterate over the characters of
# the string
for i in range(N):
if (S[i] == '0'):
break
Y += 1
# Iterate until i is greater than
# or equal to 0
i = N - 1
while(i >= 0):
if (S[i] == '1'):
break
X += 1
# If N is equal to x+y
if (N == X + Y):
return 0
# Return answer
return N - (X + Y) - 1
# Driver code
if __name__ == '__main__':
# Input
S = "001111"
N = len(S)
# Function call
print(maxOperations(S, N))
# This code is contributed by SURENDRA_GANGWAR
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the maximum moves
// that can be performed on a string
static int maxOperations(String S, int N)
{
// Stores 0s in suffix
int X = 0;
// Stores 1s in prefix
int Y = 0;
// Iterate over the characters of
// the string
for(int i = 0; i < N; i++)
{
if (S[i] == '0')
break;
Y++;
}
// Iterate until i is greater than
// or equal to 0
for(int i = N - 1; i >= 0; i--)
{
if (S[i] == '1')
break;
X++;
}
// If N is equal to x+y
if (N == X + Y)
return 0;
// Return answer
return N - (X + Y) - 1;
}
// Driver code
static void Main()
{
// Input
String S = "001111";
int N = S.Length;
// Function call
Console.WriteLine(maxOperations(S, N));
}
}
// This code is contributed by abhinavjain194
Javascript
输出
5
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。