# Minimum replacements such that no palindromic substring of length exceeding 1 is present in the given string

• Difficulty Level : Easy
• Last Updated : 12 May, 2021

Given a string str consisting of lowercase characters, the task is to modify the string such that it does not contain any palindromic substring of length exceeding 1 by minimum replacement of characters.

Examples:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: str = “bbbbbbb”
Output: 4
String can be modified to “bacbacb” by replacing 4 characters.

Input: str = “geeksforgeeks”
Output: 2

Approach:

To solve the problem, the idea is that, if there exists a palindrome of length larger than 3, then there exists a palindrome of length 2 or 3. Therefore, greedily remove all palindromes of length 2 or 3. Follow the steps below to solve the problem:

• Initialize a variable, say change, to store the required number of replacements.
• Iterate over the characters of the given string and perform the following steps:
• If the character at the current index is the same as the character at the next index, then increment change by 1.
• Otherwise, check if the character at the previous index is the same as the character at the next index, i.e. there is a palindromic substring of length 3. Therefore, increment change by 1.

Below is the implementation of the above approach:

## C++

 `// C++ Program to implement``// the above approach` `#include ``using` `namespace` `std;` `// Function to count the changes required such``// that no palindromic substring of length``// exceeding 1 is present in the string``int` `maxChange(string str)``{` `    ``// Base Case``    ``if` `(str.size() <= 1) {``        ``return` `0;``    ``}` `    ``// Stores the count``    ``int` `minChanges = 0;` `    ``// Iterate over the string``    ``for` `(``int` `i = 0; i < str.size() - 1; i++) {` `        ``// Palindromic Substring of Length 2``        ``if` `(str[i] == str[i + 1]) {` `            ``// Replace the next character``            ``str[i + 1] = ``'N'``;` `            ``// Increment changes``            ``minChanges += 1;``        ``}``        ``// Palindromic Substring of Length 3``        ``else` `if` `(i > 0 && str[i - 1] == str[i + 1]) {` `            ``// Replace the next character``            ``str[i + 1] = ``'N'``;``            ``// Increment changes``            ``minChanges += 1;``        ``}``    ``}` `    ``return` `minChanges;``}` `// Driver Code``int` `main()``{``    ``string str = ``"bbbbbbb"``;``    ``cout << maxChange(str);``    ``return` `0;``}`

## Java

 `// Java Program to implement``// the above approach``import` `java.util.*;``class` `GFG``{` `// Function to count the changes required such``// that no palindromic subString of length``// exceeding 1 is present in the String``static` `int` `maxChange(``char` `[]str)``{` `    ``// Base Case``    ``if` `(str.length <= ``1``)``    ``{``        ``return` `0``;``    ``}` `    ``// Stores the count``    ``int` `minChanges = ``0``;` `    ``// Iterate over the String``    ``for` `(``int` `i = ``0``; i < str.length - ``1``; i++)``    ``{` `        ``// Palindromic SubString of Length 2``        ``if` `(str[i] == str[i + ``1``])``        ``{` `            ``// Replace the next character``            ``str[i + ``1``] = ``'N'``;` `            ``// Increment changes``            ``minChanges += ``1``;``        ``}``      ` `        ``// Palindromic SubString of Length 3``        ``else` `if` `(i > ``0` `&& str[i - ``1``] == str[i + ``1``])``        ``{` `            ``// Replace the next character``            ``str[i + ``1``] = ``'N'``;``          ` `            ``// Increment changes``            ``minChanges += ``1``;``        ``}``    ``}``    ``return` `minChanges;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``String str = ``"bbbbbbb"``;``    ``System.out.print(maxChange(str.toCharArray()));``}``}` `// This code is contributed by shikhasingrajput`

## Python3

 `# Python3 Program to implement``# the above approach` `# Function to count the changes required such``# that no palindromic subof length``# exceeding 1 is present in the string``def` `maxChange(``str``):``    ``str` `=` `[i ``for` `i ``in` `str``]``    ``if` `(``len``(``str``) <``=` `1``):``        ``return` `0` `    ``# Stores the count``    ``minChanges ``=` `0` `    ``# Iterate over the string``    ``for` `i ``in` `range``(``len``(``str``) ``-` `1``):` `        ``# Palindromic Subof Length 2``        ``if` `(``str``[i] ``=``=` `str``[i ``+` `1``]):` `            ``# Replace the next character``            ``str``[i ``+` `1``] ``=` `'N'` `            ``# Increment changes``            ``minChanges ``+``=` `1``            ` `        ``# Palindromic Subof Length 3``        ``elif` `(i > ``0` `and` `str``[i ``-` `1``] ``=``=` `str``[i ``+` `1``]):` `            ``# Replace the next character``            ``str``[i ``+` `1``] ``=` `'N'``            ` `            ``# Increment changes``            ``minChanges ``+``=` `1``    ``return` `minChanges` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``str` `=` `"bbbbbbb"``    ``print` `(maxChange(``str``))` `# This code is contributed by mohit kumar 29.`

## C#

 `// C# Program to implement``// the above approach``using` `System;` `public` `class` `GFG``{` `// Function to count the changes required such``// that no palindromic subString of length``// exceeding 1 is present in the String``static` `int` `maxChange(``char` `[]str)``{` `    ``// Base Case``    ``if` `(str.Length <= 1)``    ``{``        ``return` `0;``    ``}` `    ``// Stores the count``    ``int` `minChanges = 0;` `    ``// Iterate over the String``    ``for` `(``int` `i = 0; i < str.Length - 1; i++)``    ``{` `        ``// Palindromic SubString of Length 2``        ``if` `(str[i] == str[i + 1])``        ``{` `            ``// Replace the next character``            ``str[i + 1] = ``'N'``;` `            ``// Increment changes``            ``minChanges += 1;``        ``}``      ` `        ``// Palindromic SubString of Length 3``        ``else` `if` `(i > 0 && str[i - 1] == str[i + 1])``        ``{` `            ``// Replace the next character``            ``str[i + 1] = ``'N'``;``          ` `            ``// Increment changes``            ``minChanges += 1;``        ``}``    ``}``    ``return` `minChanges;``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``String str = ``"bbbbbbb"``;``    ``Console.Write(maxChange(str.ToCharArray()));``}``}` ` `  `// This code contributed by shikhasingrajput`

## Javascript

 ``

Output:
`4`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up