Given a string str and the task is to modify the string such that no three consecutive characters are same. In a single operation, any character can be inserted at any position in the string. Find the minimum number of such operations required.
Input : str = “aabbbcc”
“aabbdbcc” is the modified string.
Input: str = “geeksforgeeks”
Approach: For every three consecutive characters which are same, a single character has to be inserted in between them to make any three consecutive characters different. But the number of operations needs to be minimized so the character must be inserted after the second character. For example, if the string is “bbbb” then if the character is inserted at the second position i.e. “babbb” then there are still three consecutive same characters and another operation is required to resolve that but if the character was inserted at the third position i.e. “bbabb” then only a single operation is required.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Strings formed from given characters without any consecutive repeating characters
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Minimum number of operations to move all uppercase characters before all lower case characters
- Minimum characters to be replaced to make frequency of all characters same
- Print consecutive characters together in a line
- Longest substring such that no three consecutive characters are same
- Check three or more consecutive identical characters or numbers
- Group consecutive characters of same type in a string
- Reduce the string by removing K consecutive identical characters
- Binary string with given frequencies of sums of consecutive pairs of characters
- Replace minimal number of characters to make all characters pair wise distinct
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
- Longest substring with atmost K characters from the given set of characters
- Minimum length substring with exactly K distinct characters
- Minimum characters to be replaced to remove the given substring
- Minimum characters to be deleted from the end to make given two strings equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.