📌  相关文章
📜  清空二进制字符串所需的连续相似字符的最小删除

📅  最后修改于: 2021-10-26 06:25:10             🧑  作者: Mango

给定一个长度为N的二进制字符串S ,任务是找到清空给定二进制字符串所需的相邻相似字符的最小移除次数。

例子:

方法:可以使用贪婪方法解决给定的问题。这个想法是删除频率更高的字符的连续出现。请按照以下步骤解决问题:

  • 遍历给定的字符串S并生成一个新字符串,比如newString ,通过删除频率更高的字符的连续出现。
  • 最后,打印(sizeof(newString) + 1)/2作为所需答案

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find minimum steps
// to make the string empty
int minSteps(string S)
{
    // Stores the modified string
    string new_str;
 
    // Size of string
    int N = S.length();
 
    int i = 0;
 
    while (i < N) {
 
        new_str += S[i];
 
        // Removing substring of same
        // character from modified string
        int j = i;
        while (i < N && S[i] == S[j])
            ++i;
    }
 
    // Print the minimum steps required
    cout << ceil((new_str.size() + 1) / 2.0);
}
 
// Driver Code
int main()
{
    // Given string S
    string S = "0010100";
 
    // Function Call
    minSteps(S);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find minimum steps
// to make the String empty
static void minSteps(String S)
{
     
    // Stores the modified String
    String new_str = "";
 
    // Size of String
    int N = S.length();
 
    int i = 0;
 
    while (i < N)
    {
        new_str += S.charAt(i);
         
        // Removing subString of same
        // character from modified String
        int j = i;
        while (i < N && S.charAt(i) == S.charAt(j))
            ++i;
    }
 
    // Print the minimum steps required
    System.out.print((int)Math.ceil(
        (new_str.length() + 1) / 2.0));
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given String S
    String S = "0010100";
 
    // Function Call
    minSteps(S);
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program for the above approach
from math import ceil
 
# Function to find minimum steps
# to make the empty
def minSteps(S):
     
    # Stores the modified string
    new_str = ""
 
    # Size of string
    N = len(S)
 
    i = 0
 
    while (i < N):
        new_str += S[i]
 
        # Removing substring of same character
        # from modified string
        j = i
        while (i < N and S[i] == S[j]):
            i += 1
 
    # Print the minimum steps required
    print(ceil((len(new_str) + 1) / 2))
 
# Driver Code
if __name__ == '__main__':
     
    # Given S
    S = "0010100"
 
    # Function Call
    minSteps(S)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find minimum steps
// to make the string empty
static void minSteps(string S)
{
     
    // Stores the modified string
    string new_str = "";
     
    // Size of string
    int N = S.Length;
 
    int i = 0;
 
    while (i < N)
    {
        new_str += S[i];
         
        // Removing substring of same
        // character from modified string
        int j = i;
         
        while (i < N && S[i] == S[j])
            ++i;
    }
 
    // Print the minimum steps required
    Console.Write((int)Math.Ceiling(
        (new_str.Length + 1) / 2.0));
}
 
// Driver Code
public static void Main()
{
     
    // Given string S
    string S = "0010100";
 
    // Function Call
    minSteps(S);
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript


输出:
3

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程