# Need help with this algorithm

• February 18th, 2014, 06:20 PM
kidatlehigh
Need help with this algorithm
I need a Java algorithm that converts a string of numbers into text. It is related to how a phone keypad works where pressing 2 three times creates the letter "c" or pressing 4 one time creates the letter "g". For example a string of numbers "44335557075557777" should decode to "help pls" where 0 equates to a space.
• February 18th, 2014, 06:31 PM
Norm
Re: Need help with this algorithm
Can you make a table of keypresses to character?
So far you've shown:
222 > 'c'
4 > 'g'
0 > ' '

Can you show how "44335557075557777" decodes to "help pls"?
• February 19th, 2014, 09:58 PM
dicdic
Re: Need help with this algorithm
Quote:

Can you show how "44335557075557777" decodes to "help pls"?
I think it was decoded the way how the cellphone writes a text (not the querty phone)

this is the table:
Key Num: values:
1 I thinks it has special characters
2 abc
3 def
4 ghi
5 jkl
6 mno
7 pqrs
8 tuv
9 wxyz
0 <space>

44 > h
33 > e
555 > l
7 > p
0 > <space>
7 > p
555 > l
7777 > s
• February 19th, 2014, 11:51 PM
ankurt
Re: Need help with this algorithm
Firstly you can extract out the consecutive occurence of numeric entry in your text and then you may have a hashmap in which for each combination of numeric values, you can have a corresponding alphabet. This means you will have 26 values in the hashmap corresponding to each alphabet, then a blank space for 0 and "/" for 1.
• February 20th, 2014, 01:24 AM
RESHAM
Re: Need help with this algorithm
you can also try using hashmap with Integer(0-9) as key and String as value and then store the 10 key value pair as you have specified. also have two arrays key[] and numOfTaps[]. so when you press something like "44335557075557777" , extract each key pressed in key[] array and the num of times it is pressed in numOfTaps[].
for the above example
key[]={4,3,5,7,0,7,5,7}
and numOfTaps={2,2,3,1,1,1,3,4}
then the String (say str) associated with key "key[i]" in your hashmap. (key[0] is 4 so it will have "ghi" as value)
and then get the character as str[numOfTaps[i]] (so u will get "h" for i=0)