Topic:
Given a digit string, return all possible letter combinations, the number could represent.
A mapping of Digit to letters (just as on the telephone buttons) is given below.
Input:digit string "Output": ["ad", "AE", "AF", "BD", "Be", "BF", "CD", "CE", "CF"].
Ideas:
No idea, come up to do ... is permutation. A bug is that after check input, add an empty string to the Res.
1 PublicList<string>lettercombinations (String digits) {2list<string> res =NewArraylist<string>();3 if(Digits = =NULL|| Digits.length () = = 0) {4 returnRes;5 }6 //bug:Add empty string first! after dealing with the Coner case.7Res.add ("");8 9 intLen =digits.length ();TenHashmap<character, string> key =NewHashmap<character, string>(); OneKey.put (' 1 ', "")); AKey.put (' 2 ', "ABC"); -Key.put (' 3 ', "Def"); -Key.put (' 4 ', "Ghi"); theKey.put (' 5 ', "JKL"); -Key.put (' 6 ', "MNO"); -Key.put (' 7 ', "PQRS"); -Key.put (' 8 ', "TUV"); +Key.put (' 9 ', "WXYZ"); - + for(inti = 0; i < Len; i++) { A Chardigit =Digits.charat (i); atString letter =key.get (digit); -Arraylist<string> newres =NewArraylist<string>(); - for(intj = 0; J < Letter.length (); J + +) { - for(String t:res) { -String subres = t +Letter.charat (j); - Newres.add (subres); in } - } tores =Newres; + } - returnRes; the}
Letter combinations of a Phone number