Topic
letter combinations for phone numbers
Give a numeric string, each number represents a letter, and return all possible combinations of letters.
Phone keypad, which represents the letters each number can represent.
Sample Example
Given"23"
Return["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
Note
The answers above are output in the order of the dictionaries, but you can choose the output sequence you like when you do the subject.
Solving
Unable to understand the answer
Backtracking means incomprehensible.
Public classSolution {/** * @paramdigits A Digital String *@returnAll posible letter combinations*/ PublicArraylist<string>lettercombinations (String digits) {//Write Your code herehashmap<integer,string> map =NewHashmap<integer,string>(); Map.put (0, ""); Map.put (1, ""); Map.put (2, "ABC"); Map.put (3, "Def"); Map.put (4, "Ghi"); Map.put (5, "JKL"); Map.put (6, "MnO"); Map.put (7, "PQRS"); Map.put (8, "TUV"); Map.put (9, "WXYZ"); ArrayList<String> result =NewArraylist<string>(); if(Digits = =NULL|| Digits.length () ==0 ) returnresult; ArrayList<Character> tmp =NewArraylist<character>(); Numtostring (DIGITS,TMP,RESULT,MAP); returnresult; } Public voidNumtostring (String digits,arraylist<character> tmp,arraylist<string> result,hashmap<integer,string >map) { if(Digits.length () ==0){ Char[] arr =New Char[Tmp.size ()]; for(intI=0;i< tmp.size (); i++) {Arr[i]=Tmp.get (i); } result.add (String.valueof (arr)); return; } Integer Curr= Integer.valueof (digits.substring (0,1)); String Letters=Map.get (Curr); for(inti = 0;i< letters.length (); i++) {Tmp.add (Letters.charat (i)); Numtostring (Digits.substring (1), Tmp,result,map); Tmp.remove (Tmp.size ()-1); } }}View Code
Lintcode Medium title: letter combinations of a phone number in alphabetical combination