Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
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 <= 26word consists of lowercase English letters.word are distinct.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Greedy
"abcde"
"xycdefghij"
letter-combinations-of-a-phone-number)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3014: Minimum Number of Pushes to Type Word I
class Solution {
public int minimumPushes(String word) {
int n = word.length();
int ans = 0, k = 1;
for (int i = 0; i < n / 8; ++i) {
ans += k * 8;
++k;
}
ans += k * (n % 8);
return ans;
}
}
// Accepted solution for LeetCode #3014: Minimum Number of Pushes to Type Word I
func minimumPushes(word string) (ans int) {
n := len(word)
k := 1
for i := 0; i < n/8; i++ {
ans += k * 8
k++
}
ans += k * (n % 8)
return
}
# Accepted solution for LeetCode #3014: Minimum Number of Pushes to Type Word I
class Solution:
def minimumPushes(self, word: str) -> int:
n = len(word)
ans, k = 0, 1
for _ in range(n // 8):
ans += k * 8
k += 1
ans += k * (n % 8)
return ans
// Accepted solution for LeetCode #3014: Minimum Number of Pushes to Type Word I
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3014: Minimum Number of Pushes to Type Word I
// class Solution {
// public int minimumPushes(String word) {
// int n = word.length();
// int ans = 0, k = 1;
// for (int i = 0; i < n / 8; ++i) {
// ans += k * 8;
// ++k;
// }
// ans += k * (n % 8);
// return ans;
// }
// }
// Accepted solution for LeetCode #3014: Minimum Number of Pushes to Type Word I
function minimumPushes(word: string): number {
const n = word.length;
let ans = 0;
let k = 1;
for (let i = 0; i < ((n / 8) | 0); ++i) {
ans += k * 8;
++k;
}
ans += k * (n % 8);
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.