Minimum Operations to Remove Characters in Frequency Order
Updated
Problem statement
You are given a string s. You want to make it empty using as few operations as possible.
In one operation, you pick a group of consecutive characters in s where every character is the same, and delete that group. The group can have any size from 1 up to the current length of s. After the deletion, the characters on the left and right of the group become neighbors.
Return the minimum number of operations needed to make s empty.
Function
minimumOperationsToRemove(s: String) → intExamples
Example 1
s = "abaca"return = 3Pick s = 1: remove "b" -> "aaca" Pick s = 1: remove "c" -> "aaa" Pick s = 3: since all the a's are together remove all a's at once. Now it is empty. We can make the input string empty in just 3 operations. So, we return 3 as the answer.
Constraints
1 ≤ s.length ≤ 100sconsists of lowercase English letters.
More Amazon interview questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
Similar questions at other companies
- Lexicographically Smallest Grid Path with One Letter ChangeInfosys · OAInfosys interview questions
- String Formation (Also for AI/ML Software Engineer Intern :)Snowflake · OASnowflake interview questions
- Longest Subsequence which is a SubstringSalesforce · OASalesforce interview questions
- Distinct MovesMeesho · OA
- Compressed Representation SizeMicrosoft · OAMicrosoft interview questions
- Find Minimum OperationsIBM · OAIBM interview questions
- String Challenge (Generic)TikTok · OATikTok interview questions
- Maximize the Lottery IDCitadel · OACitadel interview questions
- Sum of All ValuesFlexport · OAFlexport interview questions
- Maximum Crayon Removal ScoreWells Fargo · OAWells Fargo interview questions