Skip to content

Instantly share code, notes, and snippets.

@computingfreak
Created December 23, 2021 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save computingfreak/011e28d0113cd3c869082d046d0dc15e to your computer and use it in GitHub Desktop.
Save computingfreak/011e28d0113cd3c869082d046d0dc15e to your computer and use it in GitHub Desktop.
public class ReverseT9 {
public static void main(String x) {
ReverseT9 phone = new ReverseT9();
System.out.println(phone.reverse_t9(x));
}
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