给定二进制字符串str ,任务是通过删除单个字符或包含与str连续的不同字符序列的最小次数来清空给定的字符串。
例子:
Input: str = “0100100111”
Output: 3
Explanation:
Removing the subsequence “010101” from the string modifies str to “0011”.
Removing the subsequence “01” from the string modifies str to “01”.
Removing the subsequence “01” from the string modifies str to “” which is an empty string.
Therefore, the required output is 3.
Input: str = “010110”
Output: 2
天真的方法:解决此问题的最简单方法是重复遍历字符串,并从字符串删除最长连续的不同连续字符的子字符串,并在每次删除后增加计数。最后,打印计数。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:可以使用贪婪技术解决问题。请按照以下步骤解决问题:
- 初始化两个变量,例如cntOne和cntZero ,以存储1 s和0 s的计数。
- 使用变量i遍历字符串并检查以下条件:
- 如果str [I] ==“0”,则递增cntZero的值,并检查是否cntOne的值大于0。如果发现为真,则递减cntOne的值。
- 如果str [I] ==“1”,则递增cntOne的值,并检查是否cntZero的值大于0。如果发现为真,则减小cntZero的值。
- 最后,打印(cntZero + cntOne)的值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
//Function to count minimum operations required
// to make the string an empty string
int findMinOperationsReqEmpStr(string str)
{
// Stores count of 1s by removing
// consecutive distinct subsequence
int cntOne = 0;
// Stores count of 0s by removing
// consecutive distinct subsequence
int cntZero = 0 ;
// Stores length of str
int N = str.length();
// Traverse the string
for (int i = 0; i < N; i++) {
// If current character
// is 0
if(str[i] == '0'){
if (cntOne) {
// Update cntOne
cntOne--;
}
// Update cntZero
cntZero++;
}
// If current character
// is 1
else{
//Update cntZero
if (cntZero) {
cntZero--;
}
// Update cntOne
cntOne++;
}
}
return (cntOne + cntZero);
}
// Driver Code
int main()
{
string str = "0100100111";
cout<< findMinOperationsReqEmpStr(str);
}
Java
// Java program to implement
// the above approach
import java.util.*;
import java.lang.*;
class GFG{
//Function to count minimum operations required
// to make the string an empty string
static int findMinOperationsReqEmpStr(String str)
{
// Stores count of 1s by removing
// consecutive distinct subsequence
int cntOne = 0;
// Stores count of 0s by removing
// consecutive distinct subsequence
int cntZero = 0;
// Stores length of str
int N = str.length();
// Traverse the string
for(int i = 0; i < N; i++)
{
// If current character
// is 0
if(str.charAt(i) == '0')
{
if (cntOne != 0)
{
// Update cntOne
cntOne--;
}
// Update cntZero
cntZero++;
}
// If current character
// is 1
else
{
// Update cntZero
if (cntZero != 0)
{
cntZero--;
}
// Update cntOne
cntOne++;
}
}
return (cntOne + cntZero);
}
// Driver code
public static void main(String[] args)
{
String str = "0100100111";
System.out.print(findMinOperationsReqEmpStr(str));
}
}
// This code is contributed by ajaykr00kj
Python3
# Python3 program to implement
# the above approach
# Function to count minimum operations
# required to make the string an empty
# string
def findMinOperationsReqEmpStr(str):
# Stores count of 1s by removing
# consecutive distinct subsequence
cntOne = 0
# Stores count of 0s by removing
# consecutive distinct subsequence
cntZero = 0
# Traverse the string
for element in str:
# If current character
# is 0
if element == '0':
if cntOne > 0:
# Update cntOne
cntOne = cntOne - 1
# Update cntZero
cntZero = cntZero + 1
# If current character
# is 1
else:
# Update cntZero
if cntZero > 0:
cntZero = cntZero - 1
# Update cntOne
cntOne = cntOne + 1
return cntOne + cntZero
# Driver code
if __name__ == "__main__":
str = "0100100111"
print(findMinOperationsReqEmpStr(str))
# This code is contributed by ajaykr00kj
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to count minimum operations required
// to make the string an empty string
static int findMinOperationsReqEmpStr(String str)
{
// Stores count of 1s by removing
// consecutive distinct subsequence
int cntOne = 0;
// Stores count of 0s by removing
// consecutive distinct subsequence
int cntZero = 0;
// Stores length of str
int N = str.Length;
// Traverse the string
for(int i = 0; i < N; i++)
{
// If current character
// is 0
if(str[i] == '0')
{
if (cntOne != 0)
{
// Update cntOne
cntOne--;
}
// Update cntZero
cntZero++;
}
// If current character
// is 1
else
{
// Update cntZero
if (cntZero != 0)
{
cntZero--;
}
// Update cntOne
cntOne++;
}
}
return (cntOne + cntZero);
}
// Driver code
public static void Main(String[] args)
{
String str = "0100100111";
Console.Write(findMinOperationsReqEmpStr(str));
}
}
// This code is contributed by 29AjayKumar
输出:
3
时间复杂度: O(N)
辅助空间: O(1)