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.
LeetCode Problem - 3014
class Solution {
// Method to calculate the minimum number of pushes needed to type a word
public int minimumPushes(String word) {
// Initializing variables for index and push count
int idx = 0;
int push = 0;
// Iterating through the characters of the word
while (idx < word.length()) {
// Incrementing push count based on the position of the character
if (idx < 8) {
push += 1;
} else if (idx > 7 && idx < 16) {
push += 2;
} else if (idx > 15 && idx < 24) {
push += 3;
} else {
push += 4;
}
// Moving to the next character
idx++;
}
// Returning the total number of pushes required
return push;
}
}
Subscribe to my newsletter
Read articles from Gulshan Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Gulshan Kumar
Gulshan Kumar
As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman. I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.