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.

**Examples:**

Input:str = “abcdef”

Output:12

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”

Output:16

**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:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Array to store how many times a button ` `// has to be pressed for typing ` `// a particular character ` `const` `int` `arr[] = { 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, ` ` ` `2, 3, 1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 4 }; ` ` ` `// Function to return the count of ` `// buttons pressed to type the given string ` `int` `countKeyPressed(string str, ` `int` `len) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// Count the key presses ` ` ` `for` `(` `int` `i = 0; i < len; i++) ` ` ` `count = count + arr[str[i] - ` `'a'` `]; ` ` ` ` ` `// Return the required count ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"abcdef"` `; ` ` ` `int` `len = str.length(); ` ` ` `cout << countKeyPressed(str, len); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG { ` ` ` ` ` `// Array to store how many times a button ` ` ` `// has to be pressed for typing ` ` ` `// a particular character ` ` ` `static` `final` `int` `arr[] = { ` `1` `, ` `2` `, ` `3` `, ` `1` `, ` `2` `, ` `3` `, ` `1` `, ` `2` `, ` `3` `, ` `1` `, ` `2` `, ` `3` `, ` `1` `, ` ` ` `2` `, ` `3` `, ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `1` `, ` `2` `, ` `3` `, ` `1` `, ` `2` `, ` `3` `, ` `4` `}; ` ` ` ` ` `// Function to return the count of ` ` ` `// buttons pressed to type the given string ` ` ` `public` `static` `int` `countKeyPressed(String str, ` `int` `len) ` ` ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// Count the key presses ` ` ` `for` `(` `int` `i = ` `0` `; i < len; i++) ` ` ` `count = count + arr[str.charAt(i) - ` `'a'` `]; ` ` ` ` ` `// Return the required count ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `String str = ` `"abcdef"` `; ` ` ` `int` `len = str.length(); ` ` ` `System.out.print(countKeyPressed(str, len)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Array to store how many times a button ` `# has to be pressed for typing ` `# a particular character ` `arr ` `=` `[ ` `1` `, ` `2` `, ` `3` `, ` `1` `, ` `2` `, ` `3` `, ` `1` `, ` `2` `, ` `3` `, ` `1` `, ` `2` `, ` `3` `, ` `1` `, ` ` ` `2` `, ` `3` `, ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `1` `, ` `2` `, ` `3` `, ` `1` `, ` `2` `, ` `3` `, ` `4` `]; ` ` ` `# Function to return the count of ` `# buttons pressed to type the given string ` `def` `countKeyPressed(string, length) : ` ` ` `count ` `=` `0` `; ` ` ` ` ` `# Count the key presses ` ` ` `for` `i ` `in` `range` `(length) : ` ` ` `count ` `+` `=` `arr[` `ord` `(string[i]) ` `-` `ord` `(` `'a'` `)]; ` ` ` ` ` `# Return the required count ` ` ` `return` `count; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `string ` `=` `"abcdef"` `; ` ` ` `length ` `=` `len` `(string); ` ` ` ` ` `print` `(countKeyPressed(string, length)); ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `class` `GFG { ` ` ` ` ` `// Array to store how many times a button ` ` ` `// has to be pressed for typing ` ` ` `// a particular character ` ` ` `static` `readonly` `int` `[] arr = { 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, ` ` ` `2, 3, 1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 4 }; ` ` ` ` ` `// Function to return the count of ` ` ` `// buttons pressed to type the given string ` ` ` `public` `static` `int` `countKeyPressed(String str, ` `int` `len) ` ` ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// Count the key presses ` ` ` `for` `(` `int` `i = 0; i < len; i++) ` ` ` `count = count + arr[str[i] - ` `'a'` `]; ` ` ` ` ` `// Return the required count ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `String str = ` `"abcdef"` `; ` ` ` `int` `len = str.Length; ` ` ` `Console.Write(countKeyPressed(str, len)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Array to store how many times a ` `// button has to be pressed for ` `// typing a particular character ` `$arr` `= ` `array` `( 1, 2, 3, 1, 2, 3, ` ` ` `1, 2, 3, 1, 2, 3, 1, ` ` ` `2, 3, 1, 2, 3, 4, 1, ` ` ` `2, 3, 1, 2, 3, 4 ); ` ` ` `// Function to return the count of ` `// buttons pressed to type the given string ` `function` `countKeyPressed(` `$str` `, ` `$len` `) ` `{ ` ` ` `global` `$arr` `; ` ` ` `$count` `= 0; ` ` ` ` ` `// Count the key presses ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$len` `; ` `$i` `++) ` ` ` `$count` `= ` `$count` `+ ` `$arr` `[ord(` `$str` `[` `$i` `]) - ` ` ` `ord(` `'a'` `)]; ` ` ` ` ` `// Return the required count ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver code ` `$str` `= ` `"abcdef"` `; ` `$len` `= ` `strlen` `(` `$str` `); ` `echo` `countKeyPressed(` `$str` `, ` `$len` `); ` ` ` `// This code is contributed by ita_c ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

12

**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.

## Recommended Posts:

- 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
- Check if count of Alphabets and count of Numbers are equal in the given String
- Count of numbers whose difference with Fibonacci count upto them is atleast 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
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count of binary strings of length N having equal count of 0's and 1's
- 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 of quadruplets with given sum | Set 3
- Count of subarrays with sum at least K
- Count rotations of N which are Odd and Even
- Count pairs with Odd XOR
- Count of Subsets containing only the given value 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 contribute@geeksforgeeks.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.