cswork / OA打开练习工作台

MathWorksOA #18

Maximum Number of Operations on String

估计中等评测数据准备中,可先阅读题面

考点(AI 标注)贪心调度哈希计数字符串

Given a string s of lowercase English characters, the following operation can be performed on it any number of times.

  • Choose three consecutive characters s[i], s[i+1] and s[i+2] where (1 ≤ i ≤ |s| - 2, 1-based indexing) such that s[i] = s[i+1] and s[i+1] ≠ s[i+2]. Replace s[i+2] with s[i].
  • For example, if s = "aabc", then after the operation at i = 1, s = "aaac". Find the maximum number of operations that can be applied to s. Function Description Complete the function maximumNumberOfOperations in the editor. maximumNumberOfOperations has the following parameter:
  • String s: the string to perform operations on Returns int: the maximum number of operations that can be applied

Constraints

len of string can be up to 10⁵

Example 1

Input:

s = "accept"

Output:

3

Explanation: The following operations are performed. Bold indicates the changed character.

  • In the original string, start at i = 2, "cce". The new string s' = "accpct".
  • Start at i = 3, s' = "acccct".
  • Start at i = 4 s' = "accccc". The maximum number of operations that can be applied is 3. (Not very sure about the output and explanation. If you happen to know about it, feel free to lmk! Manyyy thanks in advance! )

Example 2

Input:

s = "accept"

Output:

3

Explanation: The following operations are performed. Bold indicates the changed character.

  • In the original string, start at i = 2, "cce". The new string s' = "accpct".
  • Start at i = 3, s' = "acccct".
  • Start at i = 4 s' = "accccc". The maximum number of operations that can be applied is 3. (Not very sure about the output and explanation. If you happen to know about it, feel free to lmk! Manyyy thanks in advance! )