Java implementations:
Import Java.util.arraylist;import Java.util.list;public class Telphone {/** * save alphabetical sequence for each number */static string[] c = new Stri Ng[] {"",//0 "",//1 "ABC",//2 "def",//3 "GHI",//4 "JKL",//5 "MNO",//6 "PQRS",//7 "TUV",//8 "WXYZ"//9};/** * Save The length of the letter sequence corresponding to each number */static int[] Total = {0, 0, 3, 3, 3, 3, 3, 4, 3, 4};p ublic static void Main (string[] args) {List<str ing> ret = find ("012"); for (String S:ret) {System.out.println (s);}} public static list<string> Find (String s) {arraylist<string> ret = new arraylist<string> (); if (s = = null Return Ret;ret.add (""); for (char Ch:s.tochararray ()) {int len = total[ch-' 0 '];//get the corresponding letter sequence length int size = Ret.size ();// RET current sizefor (int i = 0; i < len; i++) {for (int j = 0; J < size; J + +) {Ret.add (Ret.get (j) + C[ch-' 0 '].charat (i)) ;}} if (Size < Ret.size ())//To avoid 0, 1 o'clock, the next statement causes Ret.size==0ret = new Arraylist<string> (ret.sublist (Size, ret.size () ), or//keep the newly added element, that is, delete the 0-size-1 element}return ret;}}
Get the corresponding string, then query, judge is not a word, practical map dictionary, can O (1) time to achieve judgment;
Programming Beauty 3.2 Phone number corresponding to the English word