Skip to main content
Back to problems
#3016
Medium Algorithms

Minimum number of pushes to type word ii

Hash Table String Greedy Sorting Counting
80.0% acceptance
Feb 25, 2026
777
80
You are given a string word containing 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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn minimum_pushes(word: String) -> i32 {
    let mut freq = [0i32; 26];
    for b in word.bytes() { freq[(b - b'a') as usize] += 1; }
    freq.sort_unstable_by(|a, b| b.cmp(a));
    freq.iter().enumerate().map(|(i, &f)| f * (i as i32 / 8 + 1)).sum()
  }
}