Created
December 23, 2021 06:37
-
-
Save computingfreak/011e28d0113cd3c869082d046d0dc15e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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