Count of buttons pressed in a keypad mobile

15 Jun, 2022
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 = 12Input: 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++
Java
Python3
C#
PHP
Javascript
// 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 characterconst 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 stringint 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 codeint main(){    string str = "abcdef";    int len = str.length();    cout << countKeyPressed(str, len);     return 0;}
// Java implementation of the approachclass 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));    }}
# Python3 implementation of the approach # Array to store how many times a button# has to be pressed for typing# a particular characterarr = [ 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 stringdef 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 codeif __name__ == "__main__" :    string = "abcdef";    length = len(string);         print(countKeyPressed(string, length)); # This code is contributed by Ryuga
// C# implementation of the approachusing 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));    }}
<?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 stringfunction 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?>
<script>  // Javascript implementation of the approach // Array to store how many times a button// has to be pressed for typing// a particular charactervar 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 stringfunction countKeyPressed(str, len){    var count = 0;     // Count the key presses    for (var i = 0; i < len; i++)        count = count + arr[str.charCodeAt(i) - 97];     // Return the required count    return count;} // Driver codevar str = "abcdef";var len = str.length;document.write(countKeyPressed(str, len)); // This code is contributed by noob2000.</script>
12
 
Time Complexity: O(n)
Auxiliary Space: O(1) 
rituraj_jain
ankthon
ukasp
noob2000
subhammahato348
school-programming
Arrays
Mathematical
Strings
Arrays
Strings
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Window Sliding Technique
Search, insert and delete in an unsorted array
What is Data Structure: Types, Classifications and Applications
Chocolate Distribution Problem
Set in C++ Standard Template Library (STL)
Program for Fibonacci numbers
C++ Data Types
Write a program to print all permutations of a given string
Coin Change | DP-7