📌  相关文章
📜  给定字符串中删除相邻重复项的最小插入次数

📅  最后修改于: 2022-05-13 01:57:07.490000             🧑  作者: Mango

给定字符串中删除相邻重复项的最小插入次数

给定一个大小为N的字符串str ,任务是在字符串中找到最小的加法数,使得没有两个连续的元素是相同的。

例子:

方法:可以使用以下给定步骤解决上述问题:

  1. 声明变量min_steps 并将其初始化为0。
  2. 使用从i=0i的 for 循环遍历字符串。
  3. 检查当前字符是否与之前的字符相同。
  4. 如果是,则将min_steps增加 1。
  5. 否则,继续循环。
  6. 打印min_steps作为答案

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum
// number of additions such that
// no two elements are the same
int minAdditions(string str)
{
    // Storing length of the string
    int len = str.size();
 
    // Variable to store
    // the number of steps needed
    int min_steps = 0;
 
    int i;
 
    // For loop to check
    // all colours in the string
    for (i = 1; i < len; i++) {
        if (str[i] == str[i - 1])
            min_steps++;
    }
 
    // Returning the number of additions
    return min_steps;
}
 
// Driver Code
int main()
{
    string str = "RRG";
    cout << minAdditions(str);
    return 0;
}


Java
// Java program for above approach
import java.util.*;
public class GFG {
 
  // Function to find the minimum
  // number of additions such that
  // no two elements are the same
  static int minAdditions(String str)
  {
 
    // Storing length of the string
    int len = str.length();
 
    // Variable to store
    // the number of steps needed
    int min_steps = 0;
 
    int i;
 
    // For loop to check
    // all colours in the string
    for (i = 1; i < len; i++) {
      if (str.charAt(i) == str.charAt(i - 1))
        min_steps++;
    }
 
    // Returning the number of additions
    return min_steps;
  }
 
  // Driver Code
  public static void main(String args[])
  {
    String str = "RRG";
 
    System.out.println(minAdditions(str));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# python3 program for the above approach
 
# Function to find the minimum
# number of additions such that
# no two elements are the same
def minAdditions(str):
 
    # Storing length of the string
    le = len(str)
 
    # Variable to store
    # the number of steps needed
    min_steps = 0
 
    # For loop to check
    # all colours in the string
    for i in range(1, le):
        if (str[i] == str[i - 1]):
            min_steps += 1
 
    # Returning the number of additions
    return min_steps
 
 
# Driver Code
if __name__ == "__main__":
 
    str = "RRG"
    print(minAdditions(str))
 
# This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
class GFG {
 
  // Function to find the minimum
  // number of additions such that
  // no two elements are the same
  static int minAdditions(string str)
  {
 
    // Storing length of the string
    int len = str.Length;
 
    // Variable to store
    // the number of steps needed
    int min_steps = 0;
 
    int i;
 
    // For loop to check
    // all colours in the string
    for (i = 1; i < len; i++) {
      if (str[i] == str[i - 1])
        min_steps++;
    }
 
    // Returning the number of additions
    return min_steps;
  }
 
  // Driver Code
  public static void Main()
  {
    string str = "RRG";
    Console.Write(minAdditions(str));
  }
}
 
// This code is contributed by ukasp.


Javascript



输出
1

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