Minimum replacements in a string to make adjacent characters unequal Last Updated : 29 Apr, 2021 Comments Improve Suggest changes Like Article Like Report Given a lowercase character string str of size N. In one operation any character can be changed into some other character. The task is to find the minimum number of operations such that no two adjacent characters are equal.Examples: Input: Str = "caaab" Output: 1 Explanation: Change the second a to any other character, let's change it to b. So the string becomes "cabab". and no two adjacent characters are equal. So minimum number of operations is 1.Input: Str = "xxxxxxx" Output: 3 Explanation: Replace 'x' at index 1, 3 and 5 to 'a', 'b', and 'c' respectively. Approach: The idea is similar to implement sliding window technique. In this, we need to find the non-overlapping substrings that have all the characters the same. Then the minimum operations will be the sum of the floor of half the length of each substring. There is no need to change a character directly. Instead, consider all substring started from any index having only one character.Now consider any substring of length l such that all the characters of that substring are equal then change floor ( l / 2) characters of this substring to some other character.So just iterate over all the characters of the string from any character ch find out the maximal length of the substring such that all the characters in that substring are equal to the character ch.Find the length l of this substring and add floor ( l / 2) to the ans.After that start from the character just next to the end of the above substring. C++14 // C++ program to find minimum // replacements in a string to // make adjacent characters unequal #include <bits/stdc++.h> using namespace std; // Function which counts the minimum // number of required operations void count_minimum(string s) { // n stores the length of the string s int n = s.length(); // ans will store the required ans int ans = 0; // i is the current index in the string int i = 0; while (i < n) { int j = i; // Move j until characters s[i] & s[j] // are equal or the end of the // string is reached while (s[j] == s[i] && j < n) { j++; } // diff stores the length of the // substring such that all the // characters are equal in it int diff = j - i; // We need atleast diff/2 operations // for this substring ans += diff / 2; i = j; } cout << ans << endl; } // Driver code int main() { string str = "caaab"; count_minimum(str); return 0; } Java // Java program to find minimum // replacements in a string to // make adjacent characters unequal import java.util.*; class GFG{ // Function which counts the minimum // number of required operations static void count_minimum(String s) { // n stores the length of the string s int n = s.length(); // ans will store the required ans int ans = 0; // i is the current index in the string int i = 0; while (i < n) { int j = i; // Move j until characters s[i] & s[j] // are equal or the end of the // string is reached while (j < n && s.charAt(j) == s.charAt(i)) { j++; } // diff stores the length of the // substring such that all the // characters are equal in it int diff = j - i; // We need atleast diff/2 operations // for this substring ans += diff / 2; i = j; } System.out.println(ans); } // Driver code public static void main(String[] args) { String str = "caaab"; count_minimum(str); } } // This code is contributed by offbeat Python3 # Python3 program to find minimum # replacements in a string to # make adjacent characters unequal # Function which counts the minimum # number of required operations def count_minimum(s): # n stores the length of the string s n = len(s) # ans will store the required ans ans = 0 # i is the current index in the string i = 0 while i < n: j = i # Move j until characters s[i] & s[j] # are equal or the end of the # string is reached while j < n and (s[j] == s[i]): j += 1 # diff stores the length of the # substring such that all the # characters are equal in it diff = j - i # We need atleast diff/2 operations # for this substring ans += diff // 2 i = j print(ans) # Driver code if __name__=="__main__": str = "caaab" count_minimum(str) # This code is contributed by rutvik_56 C# // C# program to find minimum // replacements in a string to // make adjacent characters unequal using System; class GFG{ // Function which counts the minimum // number of required operations static void count_minimum(string s) { // n stores the length of the string s int n = s.Length; // ans will store the required ans int ans = 0; // i is the current index in the string int i = 0; while (i < n) { int j = i; // Move j until characters s[i] & s[j] // are equal or the end of the // string is reached while (j < n && s[j] == s[i]) { j++; } // diff stores the length of the // substring such that all the // characters are equal in it int diff = j - i; // We need atleast diff/2 operations // for this substring ans += diff / 2; i = j; } Console.WriteLine(ans); } // Driver code static void Main() { string str = "caaab"; count_minimum(str); } } // This code is contributed by divyeshrabadiya07 JavaScript <script> // JavaScript program to find minimum // replacements in a string to // make adjacent characters unequal // Function which counts the minimum // number of required operations function count_minimum(s) { // n stores the length of the string s var n = s.length; // ans will store the required ans var ans = 0; // i is the current index in the string var i = 0; while (i < n) { var j = i; // Move j until characters s[i] & s[j] // are equal or the end of the // string is reached while (s[j] === s[i] && j < n) { j++; } // diff stores the length of the // substring such that all the // characters are equal in it var diff = j - i; // We need atleast diff/2 operations // for this substring ans += parseInt(diff / 2); i = j; } document.write(ans + "<br>"); } // Driver code var str = "caaab"; count_minimum(str); </script> Output1 Time Complexity: O (N) Auxiliary Space: O (1) Comment More infoAdvertise with us Next Article Minimum replacements in a string to make adjacent characters unequal S shobhitgupta907 Follow Improve Article Tags : Strings DSA sliding-window substring Practice Tags : sliding-windowStrings Similar Reads Minimum replacements to make adjacent characters unequal in a ternary string Given a string of '0', '1' and '2'. The task is to find the minimum number of replacements such that the adjacent characters are not equal. Examples: Input: s = "201220211" Output: 2 Resultant string after changes is 201210210 Input: s = "0120102" Output: 0 Approach: The following problem can be sol 7 min read Minimum replacements to make adjacent characters unequal in a ternary string | Set-2 Given a string of â0â, â1â, and â2â. The task is to find the minimum number of replacements such that the adjacent characters are not equal. Examples: Input: s = â201220211â Output: 2 Resultant string after changes is 201210210 Input: s = â0120102â Output: 0 Approach: The problem has been solved usi 15+ min read Minimize replacement of characters to its nearest alphabet to make a string palindromic Given a string S of length N consisting of lowercase alphabets, the task is to find the minimum number of operations to convert the given string into a palindrome. In one operation, choose any character and replace it by its next or previous alphabet. Note: The alphabets are cyclic i.e., if z is inc 6 min read Remove k characters in a String such that ASCII sum is minimum Given a string s and an integer k, the task is to remove k characters from the string such that the sum of ASCII (American Standard Code for Information Interchange) values of the remaining characters is minimized. Examples: Input: s = "ABbc", k = 2Output: 131Explanation: We need to remove exactly 2 7 min read Minimum number of characters to be replaced to make a given string Palindrome Given string str, the task is to find the minimum number of characters to be replaced to make a given string palindrome. Replacing a character means replacing it with any single character in the same position. We are not allowed to remove or add any characters. If there are multiple answers, print t 5 min read Like