Problem · String
Maximum String Operations
Learn this problemProblem statement
Given a string s of lowercase English characters, the following operation can be performed 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].
Find the maximum number of operations that can be applied to s.
Function
getMaximumOperations(s: String) → longExamples
Example 1
s = "accept"return = 3The following operations are performed (bold indicates changed character):
- Start at
i = 2,"cce": The new strings' = "acccpt". - Start at
i = 3,s' = "acccct". - Start at
i = 4,s' = "accccc".
No other selections are available. The operation can be applied a maximum of 3 times.
Constraints
3 ≤ length of s ≤ 2 * 10^5- The string
sonly contains lowercase English letters.