Minimum number of deletions so that no two consecutive are same - GeeksforGeeks

01 Feb, 2022
Given a string, find minimum number of deletions required so that there will be no two consecutive repeating characters in the string.Examples:  
Input : AAABBB
Output : 4
Explanation : New string should be AB

Input : ABABABAB
Output : 0
Explanation : There are no consecutive repeating characters.
 
If there are n consecutive same characters delete n-1 out of those n characters.  
C++
Java
Python3
C#
PHP
Javascript
// CPP code to count minimum deletions required// so that there are no consecutive characters left#include <bits/stdc++.h>using namespace std; int countDeletions(string str){    int ans = 0;    for (int i = 0; i < str.length() - 1; i++)                // If two consecutive characters are        // the same, delete one of them.        if (str[i] == str[i + 1])            ans++;            return ans;} // Driver codeint main(){    string str = "AAABBB";     // Function call to print answer    cout << countDeletions(str);     return 0;}
// Java code to count minimum deletions required// so that there are no consecutive characters leftimport java.util.*; class GFG{  static int countDeletions(String s)  {    int ans = 0;    char[] str = s.toCharArray();     for (int i = 0; i < str.length - 1; i++)                 // If two consecutive characters are        // the same, delete one of them.        if (str[i] == str[i + 1])            ans++;                          return ans;  }     // Driver code    public static void main(String[] args)    {      String str = "AAABBB";      // Function call to print answer     System.out.println(countDeletions(str));    }}/* This code is contributed by Mr. Somesh Awasthi */
# Python code to count minimum deletions required# so that there are no consecutive characters left\ def countDeletions(string):    ans = 0    for i in range(len(string) - 1):                 # If two consecutive characters are        # the same, delete one of them.        if (string[i] == string[i + 1]):            ans += 1             return ans  # Driver codestring = "AAABBB" # Function call to print answerprint(countDeletions(string)) # This code is contributed by Sachin Bisht
// C# Program to count minimum deletions// required so that there are no// consecutive characters leftusing System; class GFG{     // Function for counting deletions   static int countDeletions(String s){    int ans = 0;    char []str = s.ToCharArray();     for (int i = 0; i < str.Length - 1; i++)                 // If two consecutive characters are        // the same, delete one of them.        if (str[i] == str[i + 1])            ans++;                     return ans;}     // Driver code    public static void Main()    {        String str = "AAABBB";             // Function call to print answer        Console.Write(countDeletions(str));    }} // This code is contributed by Nitin Mittal.
<?php// PHP code to count minimum// deletions required so that// there are no consecutive// characters left function countDeletions($str){    $ans = 0;    for ($i = 0; $i < strlen($str) - 1; $i++)                 // If two consecutive characters are        // the same, delete one of them.        if ($str[$i] == $str[$i + 1])            $ans++;         return $ans;}     // Driver Code    $str = "AAABBB";     // Function call to    // print answer    echo countDeletions($str);     // This code is contributed by nitin mittal?>
<script>// javascript code to count minimum deletions required// so that there are no consecutive characters left    function countDeletions(s) {        var ans = 0;        var str = s;         for (var i = 0; i < str.length - 1; i++)             // If two consecutive characters are            // the same, delete one of them.            if (str[i] == str[i + 1])                ans++;         return ans;    }     // Driver code        var str = "AAABBB";                 // Function call to print answer        document.write(countDeletions(str));         // This code is contributed by gauravrajput1</script>
Output:  
4
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
GauravRajput1
simmytarika5
amartyaghoshgfg
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Check for Balanced Brackets in an expression (well-formedness) using Stack
KMP Algorithm for Pattern Searching
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++
Check whether two strings are anagram of each other
Longest Palindromic Substring | Set 1
Caesar Cipher in Cryptography
Top 50 String Coding Problems for Interviews
Length of the longest substring without repeating characters
How to split a string in C/C++, Python and Java?