📌  相关文章
📜  最小化使给定字符串的所有字符均等所需的插入

📅  最后修改于: 2021-04-17 15:23:37             🧑  作者: Mango

给定长度为N的二进制字符串S ,任务是根据以下条件找到需要插入的最小字符数,以使字符串中的所有字符都变为相同:

例子:

方法:想法是基于以下观察结果,通过贪婪方法解决此问题:

  • 可以看出,在此操作中,将“ 1”“ 0”的一个连续部分取反会使部分的数目减少一。因此,重复此操作以将其全部分割为一个部分就足够了。所需的操作数等于-1的部分
  • 用更简单的术语来说,计算不相等的相邻字符对的总数,以便反转其中一个可以将整个子字符串转换为相似的子字符串。

请按照以下步骤解决问题:

  • 初始化一个变量,例如count ,该变量存储不同相邻字符的计数。
  • 遍历字符串并检查当前字符和下一个字符是否不同,然后增加count的值。
  • 完成上述步骤后,将count的值打印为所需的最少操作。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate the minimum
// number of operations required to make
// all characters of the string same
int minOperations(string& S)
{
    // Stores count of operations
    int count = 0;
 
    // Traverse the string
    for (int i = 1; i < S.length(); i++) {
 
        // Check if adjacent
        // characters are same or not
        if (S[i] != S[i - 1]) {
 
            // Increment count
            count += 1;
        }
    }
 
    // Print the count obtained
    cout << count;
}
 
// Driver Code
int main()
{
    string S = "0101010101";
    minOperations(S);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
class GFG
{
   
  // Function to calculate the minimum
  // number of operations required to make
  // all characters of the string same
  static void minOperations(String S)
  {
 
    // Stores count of operations
    int count = 0;
 
    // Traverse the string
    for (int i = 1; i < S.length(); i++)
    {
 
      // Check if adjacent
      // characters are same or not
      if (S.charAt(i) != S.charAt(i - 1))
      {
 
        // Increment count
        count += 1;
      }
    }
 
    // Print the count obtained
     System.out.print(count);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    String S = "0101010101";
    minOperations(S);
  }
}
 
// This code is contributed by susmitakundugoaldanga.


Python3
# Python program to implement
# the above approach
 
# Function to calculate the minimum
# number of operations required to make
# all characters of the string same
def minOperations(S):
   
  # Stores count of operations
    count = 0;
 
    # Traverse the string
    for i in range(1, len(S)):
 
        # Check if adjacent
        # characters are same or not
        if (S[i] != S[i - 1]):
           
            # Increment count
            count += 1;
 
    # Prthe count obtained
    print(count);
 
# Driver Code
if __name__ == '__main__':
    S = "0101010101";
    minOperations(S);
 
# This code is contributed by 29AjayKumar


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG
{
 
  // Function to calculate the minimum
  // number of operations required to make
  // all characters of the string same
  static void minOperations(string S)
  {
 
    // Stores count of operations
    int count = 0;
 
    // Traverse the string
    for (int i = 1; i < S.Length; i++)
    {
 
      // Check if adjacent
      // characters are same or not
      if (S[i] != S[i - 1])
      {
 
        // Increment count
        count += 1;
      }
    }
 
    // Print the count obtained
    Console.Write(count);
  }
 
  // Driver Code
  public static void Main()
  {
    string S = "0101010101";
    minOperations(S);
  }
}
 
// This code is contributed by code_hunt.


输出:
9

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