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++

`// 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 code ` `int` `main() ` `{ ` ` ` `string str = ` `"AAABBB"` `; ` ` ` ` ` `// Function call to print answer ` ` ` `cout << countDeletions(str); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java code to count minimum deletions required ` `// so that there are no consecutive characters left ` `import` `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 */` |

*chevron_right*

*filter_none*

## Python

`# 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 code ` `string ` `=` `"AAABBB"` ` ` `# Function call to print answer ` `print` `countDeletions(string) ` ` ` `# This code is contributed by Sachin Bisht ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to count minimum deletions ` `// required so that there are no ` `// consecutive characters left ` `using` `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. ` |

*chevron_right*

*filter_none*

## PHP

`<?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 ` `?> ` |

*chevron_right*

*filter_none*

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 contribute.geeksforgeeks.org or mail your article to contribute@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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum Cost of deletions such that string does not contains same consecutive characters
- Minimum number of deletions to make a string palindrome
- Minimum number of deletions to make a string palindrome | Set 2
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Deletions of "01" or "10" in binary string to make it free from "01" or "10"
- Maximum sum and product of the M consecutive digits in a number
- Lexicographical smallest number after at most K consecutive swaps
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Minimum number of distinct powers of 2 required to express a given binary number
- Minimum number of stops from given path
- Form minimum number from given sequence
- Minimum number of sub-strings of a string such that all are power of 5
- Find the minimum number possible by changing at most one digit
- Transform One String to Another using Minimum Number of Given Operation
- Minimum number of times A has to be repeated such that B is a substring of it
- Find minimum number of steps to reach the end of String