Posts Convert a sentence into its equivalent mobile numeric keypad sequence (geeksforgeeks - SDE Sheet)
Post
Cancel

Convert a sentence into its equivalent mobile numeric keypad sequence (geeksforgeeks - SDE Sheet)

PROBLEM DESCRIPTION

Given a sentence in the form of a string in uppercase, convert it into its equivalent mobile numeric keypad sequence. Please note there might be spaces in between the words in a sentence and we can print spaces by pressing 0.

geeksforgeeks

SOLUTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution
{
    String printSequence(String S)
    {

        int[] chars = {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};

        StringBuffer sb = new StringBuffer();

        for(int i=0; i<S.length(); i++){

            if(S.charAt(i) == ' '){
                sb.append("0");
            }else
                sb.append(chars[S.charAt(i) - 'A']);

        }

        return sb.toString();

    }
}
This post is licensed under CC BY 4.0 by the author.