Package Level3; import java. util. arrayList;/*** Letter Combinations of a Phone Number *** Given a digit string, return all possible letter combinations that the number cocould represent. A mapping of digit to letters (just like on the telephone buttons) is given below. http://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Telephone-keypad2.svg/200px-Telephone-keypad2.svg.pngInput:Digit string "23" Output: ["ad", "AE", "af", "bd", "be", "bf", "cd", "ce ", "cf"]. note: Although the above answer is in lexicographical order, your answer cocould be in any order you want. **/public class S17 {public static void main (String [] args) {System. out. println (letterCombinations ("23");} public static ArrayList <String> letterCombinations (String digits) {String [] ss = {"", "", "abc ", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz "}; arrayList <String> ret = new ArrayList <String> (); rec (ret, digits. length (), ss, digits, new StringBuffer (); return ret;} public static void rec (ArrayList <String> ret, int remain, String [] ss, String digits, stringBuffer sb) {// indicates the end of digits traversal. if (remain = 0) {ret. add (sb. toString (); return;} String s = ss [digits. charAt (0)-'0']; // obtain the string s corresponding to digits [0] // DFSfor (int I = 0; I <s. length (); I ++) {sb = sb. append (s. charAt (I); rec (ret, remain-1, ss, digits. substring (1), sb); sb. deleteCharAt (sb. length ()-1); // restore site }}}