Given a string str, the task is to count how many times the keys will be pressed in total if there is a mobile like below image to form the given string.
Input: str = “abcdef”
1 for a, 2 for b, 3 for c, 1 for d, 2 for e and 3 for f
Total = 1 + 2 + 3 + 1 + 2 + 3 = 12
Input: str = “ssss”
Approach: Use an array to store how many times a button has to be pressed for typing a particular character and then traverse the given string character by character and add all the corresponding count of key presses to the sum variable.
Below is the implementation of the above approach:
Time Complexity: O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Mobile Numeric Keypad Problem
- Convert a sentence into its equivalent mobile numeric keypad sequence
- Check whether the string can be printed using same row of qwerty keypad
- Check if a given mobile number is fancy
- CBSE Class 11 | Mobile Operating Systems - Symbian, Andriod and iOS
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count of numbers whose difference with Fibonacci count upto them is atleast K
- Check if count of Alphabets and count of Numbers are equal in the given String
- Count of binary strings of length N having equal count of 0's and 1's
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count of all values of N in [L, R] such that count of primes upto N is also prime
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Longest subarray having count of 1s one more than count of 0s
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Check if count of even divisors of N is equal to count of odd divisors
- Count the triplets such that A[i] < B[j] < C[k]
- Count pairs with Odd XOR
- Count of all possible values of X such that A % X = B
- Count of quadruplets with given sum | Set 3
- Count of subarrays with sum at least K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.