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

📅  最后修改于: 2021-09-06 06:21:20             🧑  作者: 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.


Javascript


输出:
9

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

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