I was working on a problem where I need to convert a given alpha-numeric String to text based format (don’t know what exactly to call it). Example: a = 2, c = 222, f = 333 etc. To make it more clear, this is found on your phone’s keypad. Like the way you text in the old hard key phones which didn’t have QWERTY keyboard.
Example: (INPUT) hello
(OUTPUT) 44+33+555+555+666
Here is what I had in mind. I can pre-load all the cases (26 + 10 over all) into a HashMap and use that. But I think there should be some other better way to do this right? All/any brilliant ideas are welcome.
HashMapwould be the simplest way to do what you wantBecause you are working with a static set of mappings, you might want to consider implementing your lookup table as a new class. I am thinking the internals of the class can just be an array of strings. When you do a lookup in the array, you can subtract the ‘a’ char from the character you are looking up and that will become the array index. (for the punctuation characters, if the result of the subtraction is not between 0 and 25, you can handle them with special cases). This method will bypass the hash function and improve performance.