给定一个二进制字符串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和0的计数。
- 使用变量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
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live