Given a lowercase characters 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:1Explanation:

Change the secondato any other character, let’s change it tob. So the string becomes “cabab”. and no two adjacent characters are equal. So minimum number of operations is 1.Input:Str = “xxxxxxx”Output:3Explanation:

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 has all the characters 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; ` `} ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

**Output**

1

**Time Complexity:** O (N)

**Auxiliary Space: **O (1)

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 replacements to make adjacent characters unequal in a ternary string
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Minimum number of replacements to make the binary string alternating | Set 2
- Minimum characters to be added at front to make string palindrome
- Minimum replacements such that the difference between the index of the same characters is divisible by 3
- Minimum number of characters to be removed to make a binary string alternate
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Convert string X to an anagram of string Y with minimum replacements
- Minimum characters to be replaced to make frequency of all characters same
- String with k distinct characters and no same characters adjacent
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Min flips of continuous characters to make all characters same in a string
- Minimum string such that every adjacent character of given string is still adjacent
- Rearrange characters in a string such that no two adjacent are same
- Rearrange characters in a string such that no two adjacent are same using hashing
- C program to swap adjacent characters of a String
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Minimum operations to make frequency of all characters equal K
- Minimum characters required to make a password strong

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.