Problem · String

Max Number of Operations

HardSalesforceOA
See Salesforce hiring insights

Given a string s, choose 3 consecutive characters s[i], s[i+1], s[i+2]. Replace s[i+2] with s[i] if s[i]=s[i+1] and s[i+1]≠s[i+2]. Find the maximum number of operations that can be performed on s.

Examples
01 · Example 1
s = "accept"
return = 3
:3
02 · Example 2
s = "aabaab"
return = 2
:o
03 · Example 3
s = "aabba"
return = 4
:)
More Salesforce problems
drafts saved locally
public int maxNumberOfOperations(String s) {
  // write your code here
}
s"accept"
expected3
sign in to submit