Given a sentence s consisting of uppercase English alphabets and spaces, convert it into its equivalent mobile numeric keypad sequence. For each character, press the corresponding keypad key as many times as its position on that key. For a space, press 0 once.
Note: s contains only uppercase English alphabets (A-Z) and spaces.

Examples:
Input: s = "GFG"
Output: 43334
Explanation: For 'G', press '4' one time. For 'F', press '3' three times. For the second 'G', press '4' one time.Input: s = "HEY U"
Output: 4433999088
Explanation: For 'H', press '4' two times. For 'E', press '3' two times. For 'Y', press '9' three times. For the space, press '0' one time. For 'U', press '8' two times.
Table of Content
[Naive Approach] Using Keypad Mapping and Press Count - O(n) Time and O(1) Space
We create two strings of size 26 each to store the keypad digit and the number of presses required for each English character.
For every character, append the corresponding keypad digit the required number of times. For a space, append 0.
Working of Approach
- Create mappings for the keypad digit and number of presses for each letter.
- Traverse the given sentence character by character.
- If the character is a space, append 0 to the answer.
- Otherwise, find its keypad digit and number of presses, then append the digit accordingly.
- Return the generated keypad sequence.
#include <iostream>
#include <string>
using namespace std;
string printSequence(string &s)
{
// Store the keypad number for each alphabet.
string keys = "22233344455566677778889999";
// Store the number of presses for each alphabet.
string presses = "12312312312312312341231234";
string res = "";
// Traverse each character of the sentence.
for (char ch : s)
{
// Convert spaces to 0 on the keypad.
if (ch == ' ')
{
res += '0';
}
else
{
// Find the index of the current alphabet.
int idx = ch - 'A';
// Get the keypad number and number of presses.
char key = keys[idx];
int cnt = presses[idx] - '0';
// Append the keypad number required times.
for (int i = 0; i < cnt; i++)
{
res += key;
}
}
}
return res;
}
int main()
{
string s = "HEY U";
cout << printSequence(s) << endl;
return 0;
}
import java.util.*;
class GFG {
public String printSequence(String s)
{
// Store the keypad number for each alphabet.
String keys = "22233344455566677778889999";
// Store the number of presses for each alphabet.
String presses = "12312312312312312341231234";
StringBuilder res = new StringBuilder();
// Traverse each character of the sentence.
for (char ch : s.toCharArray()) {
// Convert spaces to 0 on the keypad.
if (ch == ' ') {
res.append('0');
}
else {
// Find the index of the current alphabet.
int idx = ch - 'A';
// Get the keypad number and number of
// presses.
char key = keys.charAt(idx);
int cnt = presses.charAt(idx) - '0';
// Append the keypad number required times.
for (int i = 0; i < cnt; i++) {
res.append(key);
}
}
}
return res.toString();
}
public static void main(String[] args)
{
GFG obj = new GFG();
String s = "HEY U";
System.out.println(obj.printSequence(s));
}
}
def printSequence(s):
# Store the keypad number for each alphabet.
keys = "22233344455566677778889999"
# Store the number of presses for each alphabet.
presses = "12312312312312312341231234"
res = ""
# Traverse each character of the sentence.
for ch in s:
# Convert spaces to 0 on the keypad.
if ch == ' ':
res += '0'
else:
# Find the index of the current alphabet.
idx = ord(ch) - ord('A')
# Get the keypad number and number of presses.
key = keys[idx]
cnt = int(presses[idx])
# Append the keypad number required times.
for i in range(cnt):
res += key
return res
if __name__ == "__main__":
s = "HEY U"
print(printSequence(s))
using System;
using System.Text;
class GFG {
public string printSequence(string s)
{
// Store the keypad number for each alphabet.
string keys = "22233344455566677778889999";
// Store the number of presses for each alphabet.
string presses = "12312312312312312341231234";
StringBuilder res = new StringBuilder();
// Traverse each character of the sentence.
foreach(char ch in s)
{
// Convert spaces to 0 on the keypad.
if (ch == ' ') {
res.Append('0');
}
else {
// Find the index of the current alphabet.
int idx = ch - 'A';
// Get the keypad number and number of
// presses.
char key = keys[idx];
int cnt = presses[idx] - '0';
// Append the keypad number required times.
for (int i = 0; i < cnt; i++) {
res.Append(key);
}
}
}
return res.ToString();
}
static void Main()
{
GFG obj = new GFG();
string s = "HEY U";
Console.WriteLine(obj.printSequence(s));
}
}
// Store the keypad number for each alphabet.
const keys = "22233344455566677778889999";
// Store the number of presses for each alphabet.
const presses = "12312312312312312341231234";
function printSequence(s)
{
let res = "";
// Traverse each character of the sentence.
for (let ch of s) {
// Convert spaces to 0 on the keypad.
if (ch === " ") {
res += "0";
}
else {
// Find the index of the current alphabet.
let idx = ch.charCodeAt(0) - "A".charCodeAt(0);
// Get the keypad number and number of presses.
let key = keys[idx];
let cnt = presses[idx].charCodeAt(0)
- "0".charCodeAt(0);
// Append the keypad number required times.
for (let i = 0; i < cnt; i++) {
res += key;
}
}
}
return res;
}
// Driver Code
const s = "HEY U";
console.log(printSequence(s));
Output
4433999088
[Expected Approach] Using Precomputed Keypad Sequences - O(n) Time and O(1) Space
- For each character, store the sequence which should be obtained at its respective position in an array, i.e. for Z, store 9999. For Y, store 999. For K, store 55 and so on.
- For each character, subtract ASCII value of 'A' and obtain the position in the array pointed
by that character and add the sequence stored in that array to a string.- If the character is a space, store 0
- Print the overall sequence.
Let us understand with an example:
Input: s = "HEY U"
- H -> keypad[7] = "44" -> res = "44"
- E -> keypad[4] = "33" -> res = "4433"
- Y -> keypad[24] = "999" -> res = "4433999"
- Space -> append 0 -> res = "44339990"
- U -> keypad[20] = "88" -> res = "4433999088"
Final Output: 4433999088
#include <iostream>
#include <string>
using namespace std;
string printSequence(string &s)
{
string keypad[] = {"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 res = "";
for (char ch : s)
{
// Convert spaces to 0 on the keypad.
if (ch == ' ')
{
res += '0';
}
else
{
// Use the character's position to find its keypad sequence.
res += keypad[ch - 'A'];
}
}
return res;
}
int main()
{
string s = "HEY U";
cout << printSequence(s) << endl;
return 0;
}
import java.util.*;
class GFG {
public String printSequence(String s)
{
String[] keypad
= { "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" };
StringBuilder res = new StringBuilder();
for (char ch : s.toCharArray()) {
// Convert spaces to 0 on the keypad.
if (ch == ' ') {
res.append('0');
}
else {
// Use the character's position to find its
// keypad sequence.
res.append(keypad[ch - 'A']);
}
}
return res.toString();
}
public static void main(String[] args)
{
GFG obj = new GFG();
String s = "HEY U";
System.out.println(obj.printSequence(s));
}
}
def printSequence(s):
# Store the complete keypad sequence for each alphabet.
keypad = [
"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"
]
res = ""
for ch in s:
# Convert spaces to 0 on the keypad.
if ch == ' ':
res += '0'
else:
# Use the character's position to find its keypad sequence.
res += keypad[ord(ch) - ord('A')]
return res
if __name__ == "__main__":
s = "HEY U"
print(printSequence(s))
using System;
using System.Text;
class GFG {
public string printSequence(string s)
{
string[] keypad
= { "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" };
StringBuilder res = new StringBuilder();
foreach(char ch in s)
{
// Convert spaces to 0 on the keypad.
if (ch == ' ') {
res.Append('0');
}
else {
// Use the character's position to find its
// keypad sequence.
res.Append(keypad[ch - 'A']);
}
}
return res.ToString();
}
public static void Main()
{
GFG obj = new GFG();
string s = "HEY U";
Console.WriteLine(obj.printSequence(s));
}
}
function printSequence(s)
{
// Store the complete keypad sequence for each alphabet.
const keypad = [
"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"
];
let res = "";
for (const ch of s) {
// Convert spaces to 0 on the keypad.
if (ch === " ") {
res += "0";
}
else {
// Use the character's position to find its
// keypad sequence.
res += keypad[ch.charCodeAt(0)
- "A".charCodeAt(0)];
}
}
return res;
}
// Driver code
const s = "HEY U";
console.log(printSequence(s));
Output
4433999088