给定一个二进制字符串str,任务是从给定的二进制字符串中删除字符的最小数量,使得剩余的字符串中的字符形成一个有序。
例子:
Input: str = “1000101”
Output: 2
Explanation:
Removal of the first two occurrences of ‘1’ modifies the string to “00001”, which is a sorted order.
Therefore, the minimum count of characters to be removed is 2.
Input: str = “001111”
Output: 0
Explanation:
The string is already sorted.
Therefore, the minimum count of character to be removed is 0.
方法:想法是计算最后一次出现0之前的1 s数和第一次出现1之后的0 s数。这两个计数中的最小值是要删除的所需字符数。步骤如下:
- 遍历字符串str并找到第一次出现1和最后一次出现0的位置。
- 如果str只有一种类型的字符,则输出0 。
- 现在,计数在最后一次出现0之前出现1的数目,并将其存储在变量cnt1中。
- 现在,计算在变量cnt0中第一次出现1之后出现的0 s的数目。
- 打印cnt0和cnt1的最小值作为要删除的最少字符数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum count
// of characters to be removed to make
// the string sorted in ascending order
int minDeletion(string str)
{
// Length of given string
int n = str.length();
// Stores the first
// occurrence of '1'
int firstIdx1 = -1;
// Stores the last
// occurrence of '0'
int lastIdx0 = -1;
// Traverse the string to find
// the first occurrence of '1'
for(int i = 0; i < n; i++)
{
if (str[i] == '1')
{
firstIdx1 = i;
break;
}
}
// Traverse the string to find
// the last occurrence of '0'
for(int i = n - 1; i >= 0; i--)
{
if (str[i] == '0')
{
lastIdx0 = i;
break;
}
}
// Return 0 if the str have
// only one type of character
if (firstIdx1 == -1 ||
lastIdx0 == -1)
return 0;
// Initialize count1 and count0 to
// count '1's before lastIdx0
// and '0's after firstIdx1
int count1 = 0, count0 = 0;
// Traverse the string to count0
for(int i = 0; i < lastIdx0; i++)
{
if (str[i] == '1')
{
count1++;
}
}
// Traverse the string to count1
for(int i = firstIdx1 + 1; i < n; i++)
{
if (str[i] == '1')
{
count0++;
}
}
// Return the minimum of
// count0 and count1
return min(count0, count1);
}
// Driver code
int main()
{
// Given string str
string str = "1000101";
// Function call
cout << minDeletion(str);
return 0;
}
// This code is contributed by bikram2001jha
Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
class GFG {
// Function to find the minimum count
// of characters to be removed to make
// the string sorted in ascending order
static int minDeletion(String str)
{
// Length of given string
int n = str.length();
// Stores the first
// occurrence of '1'
int firstIdx1 = -1;
// Stores the last
// occurrence of '0'
int lastIdx0 = -1;
// Traverse the string to find
// the first occurrence of '1'
for (int i = 0; i < n; i++) {
if (str.charAt(i) == '1') {
firstIdx1 = i;
break;
}
}
// Traverse the string to find
// the last occurrence of '0'
for (int i = n - 1; i >= 0; i--) {
if (str.charAt(i) == '0') {
lastIdx0 = i;
break;
}
}
// Return 0 if the str have
// only one type of character
if (firstIdx1 == -1
|| lastIdx0 == -1)
return 0;
// Initialize count1 and count0 to
// count '1's before lastIdx0
// and '0's after firstIdx1
int count1 = 0, count0 = 0;
// Traverse the string to count0
for (int i = 0; i < lastIdx0; i++) {
if (str.charAt(i) == '1') {
count1++;
}
}
// Traverse the string to count1
for (int i = firstIdx1 + 1; i < n; i++) {
if (str.charAt(i) == '1') {
count0++;
}
}
// Return the minimum of
// count0 and count1
return Math.min(count0, count1);
}
// Driver Code
public static void main(String[] args)
{
// Given string str
String str = "1000101";
// Function Call
System.out.println(minDeletion(str));
}
}
Python3
# Python3 program for the above approach
# Function to find the minimum count
# of characters to be removed to make
# the string sorted in ascending order
def minDeletion(s):
# Length of given string
n = len(s)
# Stores the first
# occurrence of '1'
firstIdx1 = -1
# Stores the last
# occurrence of '0'
lastIdx0 = -1
# Traverse the string to find
# the first occurrence of '1'
for i in range(0, n):
if (str[i] == '1'):
firstIdx1 = i
break
# Traverse the string to find
# the last occurrence of '0'
for i in range(n - 1, -1, -1):
if (str[i] == '0'):
lastIdx0 = i
break
# Return 0 if the str have
# only one type of character
if (firstIdx1 == -1 or
lastIdx0 == -1):
return 0
# Initialize count1 and count0 to
# count '1's before lastIdx0
# and '0's after firstIdx1
count1 = 0
count0 = 0
# Traverse the string to count0
for i in range(0, lastIdx0):
if (str[i] == '1'):
count1 += 1
# Traverse the string to count1
for i in range(firstIdx1 + 1, n):
if (str[i] == '1'):
count0 += 1
# Return the minimum of
# count0 and count1
return min(count0, count1)
# Driver code
# Given string str
str = "1000101"
# Function call
print(minDeletion(str))
# This code is contributed by Stream_Cipher
C#
// C# program for the above approach
using System.Collections.Generic;
using System;
class GFG{
// Function to find the minimum count
// of characters to be removed to make
// the string sorted in ascending order
static int minDeletion(string str)
{
// Length of given string
int n = str.Length;
// Stores the first
// occurrence of '1'
int firstIdx1 = -1;
// Stores the last
// occurrence of '0'
int lastIdx0 = -1;
// Traverse the string to find
// the first occurrence of '1'
for(int i = 0; i < n; i++)
{
if (str[i] == '1')
{
firstIdx1 = i;
break;
}
}
// Traverse the string to find
// the last occurrence of '0'
for(int i = n - 1; i >= 0; i--)
{
if (str[i] == '0')
{
lastIdx0 = i;
break;
}
}
// Return 0 if the str have
// only one type of character
if (firstIdx1 == -1 ||
lastIdx0 == -1)
return 0;
// Initialize count1 and count0 to
// count '1's before lastIdx0
// and '0's after firstIdx1
int count1 = 0, count0 = 0;
// Traverse the string to count0
for(int i = 0; i < lastIdx0; i++)
{
if (str[i] == '1')
{
count1++;
}
}
// Traverse the string to count1
for(int i = firstIdx1 + 1; i < n; i++)
{
if (str[i] == '1')
{
count0++;
}
}
// Return the minimum of
// count0 and count1
return Math.Min(count0, count1);
}
// Driver Code
public static void Main()
{
// Given string str
string str = "1000101";
// Function call
Console.WriteLine(minDeletion(str));
}
}
// This code is contributed by Stream_Cipher
Javascript
输出
2
时间复杂度: O(N)
辅助空间: O(1)