Letter Candles
Updated
Problem statement
Your friend Alice has a box with N letter candles in it. The cost of the box is determined as follows - Find the number of occurrences of each character in the box and sum up the squares of these numbers.
Alice wants to reduce the cost of the box by removing some candles from it. However, she is allowed to remove at most M candles from the box. Can you help Alice determine the minimum cost of the box?
Input
The first line of the input contains the integer N, representing the number of letter candles. The second line of the input contains the integer M, representing the number of candles Alice can remove. The third line of the input contains an N-lettered string S, which contains lowercase English letters, representing the letter candles in the box.
Output
Print the minimum possible cost of the box.
Function
minimumCost(n: int, m: int, s: String) → intExamples
Example 1
n = 6m = 2s = "bacaac"return = 6There are two As, one B, and three Cs in the box. Current cost of the box is 2^2 + 1^2 + 3^2 = 14. The best way to minimize the cost of the box is to remove two C-shaped candles from it. The new minimal cost will be 2^2 + 1^2 + 1^2 = 6. The answer is 6.
Example 2
n = 15m = 3s = "xxxxxxxxxxxxxxx"return = 144There are 15 Xs. The current cost of the box is 15^2 = 225. The only way to minimize the cost is by reducing three X-shaped candles from it. The new minimal cost will be 12^2 = 144. The answer is 144.
Constraints
Unknwon for now. Will add once find them 🛵
More Intuit interview questions
- Number of IslandsONSITE INTERVIEW · Seen Nov 2026
- VIP and Regular Service SchedulerPHONE SCREEN · Seen Nov 2026
- Employee Promotion OpportunitiesOA · Seen Oct 2026
- Next Pascal Row CoefficientOA · Seen Oct 2026
- Parse Router LogsOA · Seen Oct 2026
- Longest Repeated Letter Block with Adjacent SwapsOA · Seen Oct 2026
- Maximum Square-Free DivisorOA · Seen Oct 2026
- Two Tribes from Taller-Person CountsOA · Seen Oct 2026
- Shortest Path from an Encoded GraphOA · Seen Sep 2026
- Oldest and Youngest EmployeesOA · Seen Sep 2026
Similar questions at other companies
- Task SchedulerAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- Minimum Cost to Merge Values into OnePayPal · OAPayPal interview questions
- Maximum K-Star SumC3.ai · OAC3.ai interview questions
- Maximum Sum of a K-StarMicrosoft · OAMicrosoft interview questions
- Maximum ScoreNVIDIA · OANVIDIA interview questions
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Task SchedulingJP Morgan Chase · OAJP Morgan Chase interview questions
- Decreasing SubsequencesGoogle · OAGoogle interview questions
- Get Maximum AmountIBM · OAIBM interview questions
- Optimize TikTok Reels ViewingTikTok · OATikTok interview questions