📌  相关文章
📜  通过删除最小子序列使给定的二进制字符串不减少

📅  最后修改于: 2021-09-04 08:28:28             🧑  作者: Mango

给定一个大小为N的二进制字符串str ,任务是找到最小子序列的长度,使得在擦除子序列后得到的字符串将是最长的连续非递减字符串。

例子 :

方法:该问题可以基于以下观察来解决:

按照给定的步骤解决问题:

  • 遍历字符串的字符。
  • 计算字符串存在的01的数量
  • 要生成形式为“0000….”的非递减子序列,所需的最小删除数是字符串中1的计数
  • 要生成“1111….”形式的非递减子序列,所需的最小删除数是字符串中0的计数
  • 要生成“0000…1111….”形式的非递减子序列,可以使用以下步骤获得所需的最小去除量:
    • 遍历字符串的字符。考虑从左边移除1并从字符串的右端移除0
    • 每次迭代后更新最小值。
  • 最后,将上述三种情况下得到的最小去除量作为要求的答案打印出来。

下面是上述方法的实现:

C++
// C++ program for
// the above approach
#include 
using namespace std;
 
// Function to return the
// length of smallest subsequence
// required to be removed to make
// the given string non-decreasing
int min_length(string str)
{
 
    // Length of the string
    int n = str.length();
 
    // Count of zeros and ones
    int total_zeros = 0;
    int total_ones = 0;
 
    // Traverse the string
    for (int i = 0; i < n; i++) {
        if (str[i] == '0')
            total_zeros++;
        else
            total_ones++;
    }
 
    // Count minimum removals to
  // obtain strings of the form
  // "00000...." or "11111..."
    int ans = min(total_zeros, total_ones);
 
    int cur_zeros = 0, cur_ones = 0;
 
    for (char x : str) {
 
        // Increment count
        if (x == '0')
            cur_zeros++;
        else
            cur_ones++;
 
        // Remove 1s and remaining 0s
        ans = min(ans, cur_ones
                  + (total_zeros - cur_zeros));
    }
 
    cout << ans;
}
 
// Driver Code
int main()
{
    string str = "10011";
    min_length(str);
 
    return 0;
}


Java
// Java program for
// the above approach
import java.io.*;
 
class GFG
{
 
  // Function to return the
  // length of smallest subsequence
  // required to be removed to make
  // the given string non-decreasing
  public static void min_length(String str)
  {
 
    // Length of the string
    int n = str.length();
 
    // Count of zeros and ones
    int total_zeros = 0;
    int total_ones = 0;
 
    // Traverse the string
    for (int i = 0; i < n; i++) {
      if (str.charAt(i) == '0'){
        total_zeros++;
      }
      else{
        total_ones++;
      }
    }
 
    // Count minimum removals to
    // obtain strings of the form
    // "00000...." or "11111..."
    int ans = Math.min(total_zeros, total_ones);
    int cur_zeros = 0, cur_ones = 0;
    for (int i = 0; i


Python3
# Python3 program for
# the above approach
 
# Function to return the
# length of smallest subsequence
# required to be removed to make
# the given string non-decreasing
def min_length(str):
   
    # Length of the string
    n = len(str)
 
    # Count of zeros and ones
    total_zeros = 0
    total_ones = 0
 
    # Traverse the string
    for i in range(n):
        if (str[i] == '0'):
            total_zeros += 1
        else:
            total_ones += 1
 
    # Count minimum removals to
  # obtain strings of the form
  # "00000...." or "11111..."
    ans = min(total_zeros, total_ones)
    cur_zeros = 0
    cur_ones = 0
    for x in str:
       
        # Increment count
        if (x == '0'):
            cur_zeros += 1
        else:
            cur_ones += 1
 
        # Remove 1s and remaining 0s
        ans = min(ans, cur_ones + (total_zeros - cur_zeros))
    print(ans)
 
# Driver Code
if __name__ == '__main__':
    str = "10011"
    min_length(str)
     
    # This code is contributed by SURENDRA_GENGWAR.


C#
// C# program for
// the above approach
using System;
 
class GFG{
     
// Function to return the
// length of smallest subsequence
// required to be removed to make
// the given string non-decreasing
public static void min_length(string str)
{
     
    // Length of the string
    int n = str.Length;
     
    // Count of zeros and ones
    int total_zeros = 0;
    int total_ones = 0;
     
    // Traverse the string
    for(int i = 0; i < n; i++)
    {
        if (str[i] == '0')
        {
            total_zeros++;
        }
        else
        {
            total_ones++;
        }
    }
     
    // Count minimum removals to
    // obtain strings of the form
    // "00000...." or "11111..."
    int ans = Math.Min(total_zeros, total_ones);
    int cur_zeros = 0, cur_ones = 0;
    for(int i = 0; i < str.Length; i++)
    {
         
        // Increment count
        char x = str[i];
        if (x == '0')
        {
            cur_zeros++;
        }
        else
        {
            cur_ones++;
        }
         
        // Remove 1s and remaining 0s
        ans = Math.Min(ans, cur_ones +
                      (total_zeros - cur_zeros));
    }
    Console.WriteLine(ans);
}
 
// Driver code
static public void Main()
{
    string str = "10011";
    min_length(str);
}
}
 
// This code is contributed by offbeat


Javascript


输出
1

时间复杂度: O(N)  
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live