Skip to content

Convert a Mobile Numeric Keypad sequence to equivalent sentence

License

Notifications You must be signed in to change notification settings

sthsuyash/Numeric-Keypad-Simulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Convert a Mobile Numeric Keypad sequence to equivalent sentence.

  • coded using JavaScript (use node.js to run)
  • Given a string sequence of size N, consisting of digits [0 – 9] and character ‘.’
  • the task is to print the string that can be obtained by pressing the mobile keypad in the given sequence.
  • The keyboard layout is:
1
2
(A,B,C)
3
(D,E,F)
4
(G,H,I)
5
(J,K,L)
6
(M,N,O)
7
(P,Q,R,S)
8
(T,U,V)
9
(W,X,Y,Z)

  Note: ‘.’ represents a break while typing.

Approach: The given problem can be solved by storing the mobile keypad mappings in an array and then traverse the string 'sequence' and convert it into its equivalent string.

Follow the steps below to solve the problem:

  • Initialize an empty string, say ans to store the required result.
  • Store the string associated to each key in the mobile keypad in an array nums[] such that nums[i] represent the set of characters on pressing the digit i.
  • Traverse the given string S using the variable i and perform the following steps:
    • If S[i] is equal to ‘.’, then increment i by 1, and continue to the next iteration.
    • Otherwise, initialize a variable count as 0 to store the count of the same characters.
    • Iterate until S[i] is equal to S[i + 1] and in each iteration check the following conditions:
      • If count is equal to 3 and S[i] is 2, 3, 4, 5, 6, or 8, then break out of the loop because keys: 2, 3, 4, 5, 6, and 8 contain the same number of characters, i.e., 4.
      • If count is equal to 4 and S[i] is 7 or 9, then break out of the loop because keys: 7 and 9 contain the same number of characters, i.e., 5.
      • Increment the value of count and i by 1.
    • If S[i] is either 7 or 9, then add the character nums[str[i]][count%5] to the string ans.
    • Otherwise, add the character nums[str[i]][count%4] to the string ans.
    • Increment the value of i by 1.
  • After completing the above steps, return the ans string from the function and print in the main function.

About

Convert a Mobile Numeric Keypad sequence to equivalent sentence

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published