Problem

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 _theminimum 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.

Examples

Example 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13

![](https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png)

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18

![](https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png)

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.

Solution

Method 1 – Greedy Key Assignment

Intuition

Since all letters are distinct, we want to assign letters to keys such that the total pushes are minimized. Assign the first 8 letters to the first position on each key, the next 8 to the second position, and so on, so that pushes are minimized.

Approach

  1. There are 8 keys (2-9). Assign letters in order: first 8 letters get 1 push, next 8 get 2 pushes, next 8 get 3 pushes, last 2 get 4 pushes.
  2. For each letter, compute its push cost by its position in the sorted word.
  3. Sum up all push costs.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <string>
#include <algorithm>
using namespace std;
class Solution {
public:
    int minimumPushes(string word) {
        int n = word.size(), res = 0;
        for (int i = 0; i < n; ++i) res += i / 8 + 1;
        return res;
    }
};
1
2
3
4
5
6
7
func minimumPushes(word string) int {
    res := 0
    for i := 0; i < len(word); i++ {
        res += i/8 + 1
    }
    return res
}
1
2
3
4
5
6
7
class Solution {
    public int minimumPushes(String word) {
        int res = 0, n = word.length();
        for (int i = 0; i < n; ++i) res += i / 8 + 1;
        return res;
    }
}
1
2
3
4
5
6
7
class Solution {
    fun minimumPushes(word: String): Int {
        var res = 0
        for (i in word.indices) res += i / 8 + 1
        return res
    }
}
1
2
3
class Solution:
    def minimumPushes(self, word: str) -> int:
        return sum(i // 8 + 1 for i in range(len(word)))
1
2
3
4
5
impl Solution {
    pub fn minimum_pushes(word: String) -> i32 {
        (0..word.len()).map(|i| i / 8 + 1).sum::<usize>() as i32
    }
}
1
2
3
4
5
6
7
class Solution {
    minimumPushes(word: string): number {
        let res = 0;
        for (let i = 0; i < word.length; ++i) res += Math.floor(i / 8) + 1;
        return res;
    }
}

Complexity

  • ⏰ Time complexity: O(n) — n = length of word, single pass.
  • 🧺 Space complexity: O(1) — constant extra space.