Maximum Number of Operations on String
Updated
Problem statement
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 to s.
Function
maximumNumberOfOperations(s: String) → int
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
Examples
Example 1
s = "accept"return = 3The 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
s = "accept"return = 3The 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
len of string can be up to 105
More MathWorks interview questions
- Earliest Time to Schedule a MeetingOA · Seen Nov 2026
- Minimum Cost to Serve Requests on a RingOA · Seen Nov 2026
- Minimum Tree Diameter After Removing K LeavesOA · Seen Nov 2026
- Cities on Shortest Delivery RoutesOA · Seen Nov 2026
- Repeated Mirror XOROA · Seen Nov 2026
- Beautiful ArrangementONSITE INTERVIEW · Seen Jul 2026
- Find Minimum Cost to Remove Array ElementsONSITE INTERVIEW · Seen Jul 2026
- Group Shifted StringsONSITE INTERVIEW · Seen Jul 2026
- Longest Valid ParenthesesONSITE INTERVIEW · Seen Jul 2026
- Balancing TeamsOA · Seen Nov 2025
Similar questions at other companies
- Maximum String OperationsMicrosoft · OAMicrosoft interview questions
- Minimum No-Repeat Segments After One Character RemovalSalesforce · OASalesforce interview questions
- Longest Non-Decreasing Substring After Pair SwapsVisa · OAVisa interview questions
- Minimum Deletion Cost for Distinct Adjacent CharactersHSBC · OAHSBC interview questions
- Minimum Binary Swaps to a PalindromeAT&T · OAAT&T interview questions
- Robot Assembly Line ConflictsGoogle · OAGoogle interview questions
- Substring RemovalJP Morgan Chase · OAJP Morgan Chase interview questions
- Minimum Additions to Make a Valid ABC StringIBM · OAIBM interview questions
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Minimum Adjacent TranspositionsEpic · OAEpic interview questions