Minimize the number of button presses needed to type a word on an old-style phone keypad by assigning letters to keys optimally.
You are typing a word using a classic mobile phone keypad where each key can hold multiple letters. The first letter assigned to a key costs 1 press to type, the second costs 2 presses, and so on.
For this problem, assume you can place the letters of the word onto the available keys in any order you want. Your goal is to choose an arrangement that minimizes the total number of button presses required to type the entire word.
Compute the minimum possible number of presses needed to type the given word.
Input Format
- A lowercase English string
word. - Each character of
wordis a letter that must be typed once for each occurrence.
Output Format
- Return an integer: the minimum total number of key presses required.
Constraints
1 <= word.length <= 500wordcontains only lowercase English letters.
Example 1
Input
word = "abcde"
Output
5
Explanation
All letters appear once, so each can be assigned to a first press on some key. Total presses = 1 + 1 + 1 + 1 + 1 = 5.
Example 2
Input
word = "aabbcc"
Output
6
Explanation
Each of the 6 characters can be arranged so every occurrence is typed with cost 1. Total presses = 6.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.