Minimum Number of Pushes to Type Word

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, the key 2 is mapped with ["a","b","c"], we 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. You need to find the minimum number of times the keys will be pushed to type the string word.

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

An example mapping of letters to keys on a telephone keypad is given below. Note that 1, *, #, and 0 do not map to any letters.


Example 1:

Input: word = "abcde"
Output: 5
Explanation: The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.


Example 2:

Input: word = "xycdefghij"
Output: 12
Explanation: The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.






Constraints:

  • 1 <= word.length <= 26

  • word consists of lowercase English letters.

  • All letters in word are distinct.




My Solution

We know that the first 8 characters can be covered with 1 push, the next 8 with 2 pushes, etc. We start with the length of word and just add the smaller of 8 and the number of characters left to process. We multiply this by a multiplier which increments at every iteration and add this to our output. At every iteration we increment our multiplier and subtract min(8,n) from n.

Time complexity is O(n) since we have n/8 iterations, but time complexity is trivial because the constraints only consider a maximum word length of 26 and all letters are distinct. Space complexity is O(1) since we only require constant extra space.

class Solution {
    public int minimumPushes(String word) {
        int mult = 1;
        int n = word.length();
        int out = 0;

        while (n > 0) {
            int presses = (n >= 8 ? 8 : n);
            out += (mult * presses);
            n -= presses;
            mult++;
        }

        return out;
    }
}
Previous
Previous

Sum of Left Leaves

Next
Next

Is Subsequence