给定一个字符串形式的句子,将其转换为等效的移动数字键盘序列。
例子 :
Input : GEEKSFORGEEKS
Output : 4333355777733366677743333557777
For obtaining a number, we need to press a
number corresponding to that character for
number of times equal to position of the
character. For example, for character C,
we press number 2 three times and accordingly.
Input : HELLO WORLD
Output : 4433555555666096667775553
按照下面给出的步骤将句子转换为其等效的移动数字键盘序列。
- 对于每个字符,将应该在其各自位置获得的序列存储在一个数组中,即对于Z,存储9999。对于Y,存储999。对于K,存储55,依此类推。
- 对于每个字符,减去 ‘A’ 的 ASCII 值,得到指向的数组中的位置
通过该字符并将存储在该数组中的序列添加到字符串。 - 如果字符是空格,则存储 0
- 打印整个序列。
下面是上述方法的实现:
C++
// C++ implementation to convert a
// sentence into its equivalent
// mobile numeric keypad sequence
#include
using namespace std;
// Function which computes the sequence
string printSequence(string arr[],
string input)
{
string output = "";
// length of input string
int n = input.length();
for (int i=0; i
Java
// Java implementation to convert a
// sentence into its equivalent
// mobile numeric keypad sequence
import java.util.*;
class GFG
{
// Function which computes the sequence
static String printSequence(String arr[],
String input)
{
String output = "";
// length of input string
int n = input.length();
for (int i = 0; i < n; i++)
{
// Checking for space
if (input.charAt(i) == ' ')
output = output + "0";
else
{
// Calculating index for each
// character
int position = input.charAt(i) - 'A';
output = output + arr[position];
}
}
// Output sequence
return output;
}
// Driver Function
public static void main(String[] args)
{
// storing the sequence in array
String str[] = {"2","22","222",
"3","33","333",
"4","44","444",
"5","55","555",
"6","66","666",
"7","77","777","7777",
"8","88","888",
"9","99","999","9999"
};
String input = "GEEKSFORGEEKS";
System.out.println(printSequence(str, input));
}
}
// This code is contributed by Gitanjali.
Python3
# Python3 implementation to convert
# a sentence into its equivalent
# mobile numeric keypad sequence
# Function which computes the
# sequence
def printSequence(arr, input):
# length of input string
n = len(input)
output = ""
for i in range(n):
# checking for space
if(input[i] == ' '):
output = output + "0"
else:
# calculating index for each
# character
position = ord(input[i]) - ord('A')
output = output + arr[position]
# output sequence
return output
# Driver code
str = ["2", "22", "222",
"3", "33", "333",
"4", "44", "444",
"5", "55", "555",
"6", "66", "666",
"7", "77", "777", "7777",
"8", "88", "888",
"9", "99", "999", "9999" ]
input = "GEEKSFORGEEKS";
print( printSequence(str, input))
# This code is contributed by upendra bartwal
C#
// C# implementation to convert a
// sentence into its equivalent
// mobile numeric keypad sequence
using System;
class GFG
{
// Function which computes the sequence
static String printSequence(string []arr,
string input)
{
string output = "";
// length of input string
int n = input.Length;
for (int i = 0; i < n; i++)
{
// Checking for space
if (input[i] == ' ')
output = output + "0";
else
{
// Calculating index for each
// character
int position = input[i] - 'A';
output = output + arr[position];
}
}
// Output sequence
return output;
}
// Driver Function
public static void Main()
{
// storing the sequence in array
string []str = {"2","22","222",
"3","33","333",
"4","44","444",
"5","55","555",
"6","66","666",
"7","77","777","7777",
"8","88","888",
"9","99","999","9999"
};
string input = "GEEKSFORGEEKS";
Console.WriteLine(printSequence(str, input));
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出 :
4333355777733366677743333557777
时间复杂度: O(n)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。