给定一个字符串str ,任务是计算如果有一个移动装置(如下图)形成给定的字符串,则总共要计算多少次键被按下。
例子:
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
方法:使用数组来存储按钮有多少次被按下输入特定的字符,然后通过字符遍历给定的字符串的字符,并添加按键的所有相应数量的总和变量。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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;
}
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));
}
}
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
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));
}
}
PHP
Javascript
输出:
12
时间复杂度: O(n)