Skip to content

Instantly share code, notes, and snippets.

@junminstorage
Created December 21, 2015 04:00
Show Gist options
  • Save junminstorage/7fa4e53f2306b2fb28d7 to your computer and use it in GitHub Desktop.
Save junminstorage/7fa4e53f2306b2fb28d7 to your computer and use it in GitHub Desktop.
print all possible strings for any phone no based on a phone pad
private final static Map<Integer, char[]> store = new HashMap<>();
static {
store.put(1, new char[]{'1'});
store.put(2, new char[]{'A', 'B', 'C'});
store.put(3, new char[]{'D', 'E', 'F'});
store.put(4, new char[]{'G', 'H', 'I'});
store.put(5, new char[]{'J', 'K', 'L'});
store.put(6, new char[]{'M', 'N', 'O'});
store.put(7, new char[]{'P', 'R', 'Q', 'S'});
}
public static void phoneNum2String(int[] phone){
char[] curr = new char[phone.length];
phoneNum2StringRec(phone, 0, curr);
}
private static void phoneNum2StringRec(int[] phone, int index, char[] curr){
int len = phone.length;
if(index==len)
System.out.println(curr);
else{
char[] chars = store.get(phone[index]);
for(char c : chars){
curr[index] = c;
phoneNum2StringRec(phone, index+1, curr);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment