3014. Minimum Number of Pushes to Type Word I LeetCode Solution
In this guide, you will get 3014. Minimum Number of Pushes to Type Word I LeetCode Solution with the best time and space complexity. The solution to Minimum Number of Pushes to Type Word I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Number of Pushes to Type Word I solution in C++
- Minimum Number of Pushes to Type Word I solution in Java
- Minimum Number of Pushes to Type Word I solution in Python
- Additional Resources
Problem Statement of 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, 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.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(26) = O(1)
3014. Minimum Number of Pushes to Type Word I LeetCode Solution in C++
class Solution {
public:
int minimumPushes(string word) {
int ans = 0;
vector<int> count(26);
for (const char c : word)
++count[c - 'a'];
ranges::sort(count, greater<>());
for (int i = 0; i < 26; ++i)
ans += count[i] * (i / 8 + 1);
return ans;
}
};
/* code provided by PROGIEZ */
3014. Minimum Number of Pushes to Type Word I LeetCode Solution in Java
class Solution {
public int minimumPushes(String word) {
int ans = 0;
int[] count = new int[26];
for (final char c : word.toCharArray())
++count[c - 'a'];
Arrays.sort(count);
for (int i = 0; i < 26; ++i)
ans += count[26 - i - 1] * (i / 8 + 1);
return ans;
}
}
// code provided by PROGIEZ
3014. Minimum Number of Pushes to Type Word I LeetCode Solution in Python
class Solution:
def minimumPushes(self, word: str) -> int:
freqs = sorted(collections.Counter(word).values(), reverse=True)
return sum(freq * (i // 8 + 1) for i, freq in enumerate(freqs))
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.