FastPrepFastPrep
Problem Brief

Maximum Number of Operations on String

FULLTIMEOA

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:

  1. String s: the string to perform operations on

Returns

int: the maximum number of operations that can be applied

1Example 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! 🧡)

2Example 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! 🧡)

Constraints

Limits and guarantees your solution can rely on.

len of string can be up to 105
public int maximumNumberOfOperations(String s) {
  // write your code here
}
Input

s

"accept"

Output

3

Sign in to submit your solution.