Convert a sentence into its equivalent mobile numeric keypad sequence | Practice | GeeksforGeeks

Given a sentence in the form of a string in uppercase, convert it into its equivalent mobile numeric keypad sequence.
 
Example 1:
Input:
S = "GFG"
Output: 43334
Explanation: For 'G' press '4' one time.
For 'F' press '3' three times.

Example 2:
Input:
S = "HEY U"
Output: 4433999088
Explanation: For 'H' press '4' two times.
â€‹For 'E' press '3' two times. For 'Y' press '9' 
three times. For white space press '0' one time.
For 'U' press '8' two times.

 
Your Task:  
You dont need to read input or print anything. Complete the function printSequence() which takes a string as input parameter and returns its equivalent mobile numeric keypad sequence as a string.
 
Expected Time Complexity: O(Length of String)
Expected Auxiliary Space: O(Length of String)
 
Constraints:
1 <= Length of String <= 105
Characters of string can be empty space or capital alphabets.
0
pratapshivendrasingh175 days ago
C++ Most optimised Solution
string printSequence(string S)
{
    unordered_map <char,string> mapping;
    mapping['A']="2";
    mapping['B']="22";
    mapping['C']="222";
    mapping['D']="3";
    mapping['E']="33";
    mapping['F']="333";
    mapping['G']="4";
    mapping['H']="44";
    mapping['I']="444";
    mapping['J']="5";
    mapping['K']="55";
    mapping['L']="555";
    mapping['M']="6";
    mapping['N']="66";
    mapping['O']="666";
    mapping['P']="7";
    mapping['Q']="77";
    mapping['R']="777";
    mapping['S']="7777";
    mapping['T']="8";
    mapping['U']="88";
    mapping['V']="888";
    mapping['W']="9";
    mapping['X']="99";
    mapping['Y']="999";
    mapping['Z']="9999";
    mapping[' ']="0";
    
    string ans;
    
    for(int i=0;i<S.length();i++)
    {
        ans=ans+mapping[S[i]];
    }
    return ans;
}
0
aahshis161 week ago
time : 0.03s
def printSequence(self,S):              dic= {'A':2,'B':22,'C':222,'D':3,'E':33,'F':333,'G':4,'H':44,'I':444,       'J':5,'K':55,'L':555,'M':6,'N':66,'O':666,'P':7,'Q':77,       'R':777,'S':7777,'T':8,'U':88,'V':888,'W':9,'Y':999,'Z':9999,'X':99,' ':0}       s=''       for i in S:           s+=str(dic[i])              return s
0
18pa1a04772 weeks ago
without hard coding
class Solution:
    def printSequence(self,S):
        d={}
        res=""
        li=["","","ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ"]
        for l in range(len(li)):
            for s in range(len(li[l])):
                d[li[l][s]]=[l,s+1]
        for l in S:
            if l==" ":
                res=res+"0"
            else:
                res=res+str(d[l][0])*d[l][1]
        return res
+1
lindan1233 weeks ago
string printSequence(string S){   unordered_map<char,string> mp;   mp['A'] = "2";   mp['B'] = "22";   mp['C'] = "222";   mp['D'] = "3";   mp['E'] = "33";   mp['F'] = "333";   mp['G'] = "4";   mp['H'] = "44";   mp['I'] = "444";   mp['J'] = "5";   mp['K'] = "55";   mp['L'] = "555";   mp['M'] = "6";   mp['N'] = "66";   mp['O'] = "666";   mp['P'] = "7";   mp['Q'] = "77";   mp['R'] = "777";   mp['S'] = "7777";   mp['T'] = "8";   mp['U'] = "88";   mp['V'] = "888";   mp['W'] = "9";   mp['X'] = "99";   mp['Y'] = "999";   mp['Z'] = "9999";   mp[' '] = "0";      string ans="";   for(int i=0;i<S.length();i++)   {       ans = ans + mp[S[i]];   }   return ans;}
 
Time Taken : 0.03sec
Cpp
 
+1
veekshithc1 month ago
javascript solution

function printSequence(str){
    let v=["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 arr = []  
    var x 
    for(let i=0;i<str.length; i++){
    arr.push(v[(str.charCodeAt(i))-65])
  
    }
    return arr
}
+1
sudarshanp49121 month ago
EASY SOLUTION:
string printSequence(string S)
{
    vector<string> v={"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 ans;
    for(int i=0;i<S.length();i++)
    {
        if(S[i]==' ') ans+="0";
        else ans+=v[S[i]-'A'];
    }
    return ans;
}
+1
madhukartemba1 month ago
JAVA SOLUTION:
class Solution 
{ 
    String printSequence(String S) 
    { 
        HashMap<Character, String> map = new HashMap<>();
        
        map.put('A', "2");
        map.put('B', "22");
        map.put('C', "222");
        map.put('D', "3");
        map.put('E', "33");
        map.put('F', "333");
        map.put('G', "4");
        map.put('H', "44");
        map.put('I', "444");
        map.put('J', "5");
        map.put('K', "55");
        map.put('L', "555");
        map.put('M', "6");
        map.put('N', "66");
        map.put('O', "666");
        map.put('P', "7");
        map.put('Q', "77");
        map.put('R', "777");
        map.put('S', "7777");
        map.put('T', "8");
        map.put('U', "88");
        map.put('V', "888");
        map.put('W', "9");
        map.put('X', "99");
        map.put('Y', "999");
        map.put('Z', "9999");
        map.put(' ', "0");
        
        StringBuilder ans = new StringBuilder();
        
        for(char ch : S.toCharArray())
        {
            ans.append(map.get(ch));
        }
        
        return ans.toString();
        
    }
}
0
gauravsingh93562 months ago


unordered_map<char, string>mp;
   char t='A';
   
   for(int i=2;i<=6;i++)
   {
       mp[t]=to_string(i);
       mp[t+1]=to_string(i)+to_string(i);
       mp[t+2]=to_string(i)+to_string(i)+to_string(i);
       t+=3;
   }
   
   for(int i=7;i<=9;i++)
   {
       if(i==8)
           continue;
       mp[t]=to_string(i);
       mp[t+1]=to_string(i)+to_string(i);
       mp[t+2]=to_string(i)+to_string(i)+to_string(i);
       mp[t+3]=to_string(i)+to_string(i)+to_string(i)+to_string(i);
       t+=7;
   }
   
   mp['T']=to_string(8);
   mp['U']=to_string(8)+to_string(8);
   mp['V']=to_string(8)+to_string(8)+to_string(8);
   
  
   mp[' ']="0";
   
   string ans="";
   for(int i=0;i<S.length();i++)
   {
       ans+=mp[S[i]];
   }
   return ans;
0
patelneer4032 months ago
unordered_map<char,string> map;   int i;   string a;   string b;   map['A']="2";   map['B']="22";   map['C']="222";   map['D']="3";   map['E']="33";   map['F']="333";   map['G']="4";   map['H']="44";   map['I']="444";   map['J']="5";   map['K']="55";   map['L']="555";   map['M']="6";   map['N']="66";   map['O']="666";   map['P']="7";   map['Q']="77";   map['R']="777";   map['S']="7777";   map['T']="8";   map['U']="88";   map['V']="888";   map['W']="9";   map['X']="99";   map['Y']="999";   map['Z']="9999";   map[' ']="0";        for(i=0;S[i]!='\0';i++)   {       b=map[S[i]];      a.append(b);          }   return a;
0
vikasnayakmmmut2 months ago
string printSequence(string S){  int n=S.length();   unordered_map<char,string>p;  string s="";    p['A']="2";     p['B']="22";      p['C']="222";       p['D']="3";        p['E']="33";        p['F']="333";         p['G']="4";          p['H']="44";           p['I']="444";            p['J']="5";             p['K']="55";                           p['L']="555";               p['M']="6";                p['N']="66";                 p['O']="666";                  p['P']="7";                   p['Q']="77";                    p['R']="777";                     p['S']="7777";                      p['T']="8";                       p['U']="88";                        p['V']="888";                         p['W']="9";                          p['X']="99";                           p['Y']="999";                            p['Z']="9999";                            p[' ']="0";                            for(int i=0;i<n;i++)                            {                                s=s+p[S[i]];                            }                            return s;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.