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)
- Mobile Numeric Keypad Problem
- Convert a sentence into its equivalent mobile numeric keypad sequence
- Check if a given mobile number is fancy
- CBSE Class 11 | Mobile Operating Systems - Symbian, Andriod and iOS
- Count numbers < = N whose difference with the count of primes upto them is > = K
- 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
- Count of subarrays with sum at least K
- Count pairs with Odd XOR
- Count the triplets such that A[i] < B[j] < C[k]
- Count rotations of N which are Odd and Even
- Count of all possible values of X such that A % X = B
- Count pairs with given sum
- Count numbers that don't contain 3
- Count Divisors of n in O(n^1/3)
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.