MathWorksOA #18
Maximum Number of Operations on String
估计中等评测数据准备中,可先阅读题面
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]ands[i+2]where (1 ≤ i ≤ |s| - 2, 1-based indexing) such thats[i] = s[i+1]ands[i+1] ≠ s[i+2]. Replaces[i+2]withs[i]. - For example, if
s = "aabc", then after the operation ati = 1,s = "aaac". Find the maximum number of operations that can be applied tos. Function Description Complete the functionmaximumNumberOfOperationsin the editor.maximumNumberOfOperationshas the following parameter: String s: the string to perform operations on Returnsint: 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! )