![]() |
VOOZH | about |
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.
1
Time Complexity: O (N)
Auxiliary Space: O (1)