Minimum Number of Pushes to Type Word I

You are given a string word containing distinct lowercase English letters.

Telephone keypads have keys mapped with distinct collections of lowercase English letters, which can be used to form words by pushing them. For example, if key 2 is mapped with ["a", "b", "c"], you need to push the key one time to type "a", two times to type "b", and three times to type "c".

It is allowed to remap the keys numbered 2 to 9 to distinct collections of letters. The keys can be remapped to any amount of letters, but each letter must be mapped to exactly one key. The keys 1, *, #, and 0 do not map to any letters.

Return the minimum number of pushes needed to type word after remapping the keys.

Example 1
Inputword = "abcde"
Output5
Each of the five distinct letters can be assigned to a different key from 2 to 9, so each requires one push for a total of 5.
Example 2
Inputword = "xycdefghij"
Output12
An optimal remapping makes eight letters cost one push and two letters cost two pushes, for a total cost of 12.

Constraints

  • 1 <= word.length <= 26
  • word consists of lowercase English letters.
  • All letters in word are distinct.

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate