Skip to content

Instantly share code, notes, and snippets.

@amrishodiq
Created June 8, 2016 03:29
Show Gist options
  • Save amrishodiq/ad9717ff94cbbc5c15222227227ab4a7 to your computer and use it in GitHub Desktop.
Save amrishodiq/ad9717ff94cbbc5c15222227227ab4a7 to your computer and use it in GitHub Desktop.
public class OldPhone {
public static void main(String[] args) {
OldPhone phone = new OldPhone();
System.out.println(phone.reverse_t9("999337777"));
}
char getCharacter(char key, int index) {
char[][] characterMapping = new char[][] {
{'+'},
{' '},
{'A', 'B', 'C'},
{'D', 'E', 'F'},
{'G', 'H', 'I'},
{'J', 'K', 'L'},
{'M', 'N', 'O'},
{'P', 'Q', 'R', 'S'},
{'T', 'U', 'V'},
{'W', 'X', 'Y', 'Z'},
};
char[] selected = characterMapping[Integer.parseInt(String.valueOf(key))];
return selected[index % selected.length];
}
String reverse_t9(String keys) {
int len = keys.length(), count = 0;
char currentChar = ' ', key;
String result = "";
for (int i=0; i<len; i++) {
key = keys.charAt(i);
if (currentChar != key) {
if (currentChar != ' ') {
result += String.valueOf(getCharacter(currentChar, count));
}
count = 0;
currentChar = key;
} else {
count++;
}
}
result += String.valueOf(getCharacter(currentChar, count));
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment