Minimum number of insertions in given String to remove adjacent duplicates - GeeksforGeeks

21 Jan, 2022
Given a string str of size N, the task is to find the minimum number of additions in the string such that no two consecutive elements are the same.
Examples:
Input: str=”rrg” Output: 1Explanation: Add an element between two r’s
Input: str=”rrrrr”Output: 4
Approach: The above problem can be solved using the below given steps:
Declare variable min_steps and initialise it by 0.Traverse the string using a for-loop from i=0 to i<N.Check if the current character is the same as the character before it.If yes, then increment min_steps by 1.Else, continue the loop.Print min_steps as the answer.
Declare variable min_steps and initialise it by 0.
Traverse the string using a for-loop from i=0 to i<N.
Check if the current character is the same as the character before it.
If yes, then increment min_steps by 1.
Else, continue the loop.
Print min_steps as the answer.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <iostream>using namespace std; // Function to find the minimum// number of additions such that// no two elements are the sameint 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 Codeint main(){    string str = "RRG";    cout << minAdditions(str);    return 0;}
// Java program for above approachimport 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 program for the above approach # Function to find the minimum# number of additions such that# no two elements are the samedef 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 Codeif __name__ == "__main__":     str = "RRG"    print(minAdditions(str)) # This code is contributed by rakeshsahni
// C# program for the above approachusing 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.
<script>// Javascript program for the above approach // Function to find the minimum// number of additions such that// no two elements are the samefunction minAdditions(str){     // Storing length of the string    let len = str.length;     // Variable to store    // the number of steps needed    let min_steps = 0;     let 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 let str = "RRG";document.write(minAdditions(str)) // This code is contributed by gfgking.</script>
  
1
 
Time Complexity: O(N)Auxiliary Space: O(1)
 
rakeshsahni
ukasp
samim2000
gfgking
kaurareen
Algo-Geek 2021
Algo Geek
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Encode given String by inserting in Matrix column-wise and printing it row-wise
Lexicographically smallest string formed by concatenating any prefix and its mirrored form
Count of adjacent pairs in given Array with even sum
Check if the given string is valid English word or not
Abbreviate given string by replacing all characters with length except the first and last
Reverse a string in Java
Longest Common Subsequence | DP-4
Write a program to reverse an array or string
Write a program to print all permutations of a given string
C++ Data Types