Last Updated: 2 Feb, 2021

Convert Sentence

Easy
Asked in companies
UST GlobalNagarro SoftwareD.E.Shaw

Problem statement

You are given a sentence in the form of a string ‘S’. You have to convert ‘S’ into its equivalent mobile numeric keypad sequence, i.e. print the sequence in such a way that if it is typed on the given keypad, the output should be the string ‘S’. A keypad for the reference is shown below.

Note:

1. There will be only lower case letters.
2. There will not be any special characters or capital letters in the input string.
3. There will be no spaces in the string.

Input Format:

The first line of the input contains an integer T denoting the number of test cases.
The first line of each test case contains one integer N, denoting the length of the string ‘S’.
The second line of each test case contains the input string ‘S’.

Output Format:

The only line of output of each test case consists of an output string, denoting the pattern which is required to print ‘S’. 
The output of each test case will be printed in a separate line.

Note:

You do not need to print anything, it has already been taken care of. Just implement the given function.

Constraints:

1 <= T <= 5
1 <= N <= 3000

Where 'T' is the number of test cases and ' N' is the length of the sentence 'S'.

Approaches

01 Approach

  • The simplest possible approach is to write how many times we have to press a particular key to get a character.
  • For each character, we can store what should be typed on the keypad. We can store this information in a string array named ‘freq’.
  • The size of freq[] will be 26 and index 0 will store the pattern for ‘a’, index 1 will store the pattern for ‘b’ and so on.. Index 25 will store the pattern for ‘z’.
  • The pattern for ‘a’ is 2, for ‘z’ is 9999, for ‘n’ is 66 and so on.
  • After storing the pattern in the array, we will take an output string and while traversing the string and we will get the pattern corresponding to the current character from the freq array and add it to our output string.