#K10846. Balanced String Replacement

    ID: 23337 Type: Default 1000ms 256MiB

Balanced String Replacement

Balanced String Replacement

You are given a string s consisting of lowercase English letters. Your task is to determine the minimum number of character replacements needed to make the string balanced. A string is considered balanced if no two adjacent characters are the same.

In one replacement, you can change any character of the string to any other character from 'a' to 'z'.

For example, consider the string \(aa\). By replacing one of the 'a's, you can obtain a balanced string like \(ab\) or \(ba\). More formally, if \(s = s_1 s_2 \dots s_n\), you need to count the number of indices \(i\) (with \(2 \le i \le n\)) where \(s_i = s_{i-1}\), as each occurrence requires a replacement.

Examples:

  • Input: aa → Output: 1
  • Input: abcdeba → Output: 0
  • Input: aabbcc → Output: 3

inputFormat

The input consists of a single line containing the string s (\(1 \leq |s| \leq 10^5\)).

The string contains only lowercase letters from 'a' to 'z'.

outputFormat

Output a single integer representing the minimum number of replacements required to make the string balanced.

## sample
aa
1

</p>