Problem Definition:
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"].
Note:
Although the above answer is in lexicographical order, your answer could are in any order you want.
Solution:
1) traversal. Iterate through each number, find the character it may point to, and stitch each character with a string that has been formed before.
For example, enter a number string of ' 23 '.
The 1._ number is ' 2 ', the possible string is [' A ', ' B ', ' C ']
The 2._ number is ' 3 ', corresponding to the characters [' d ', ' e ', ' f '], respectively, and 1._ formed in the concatenation of all strings, get: [' ad ', ' AE ', ' af ', ' BD ', ' be ', ' BF ', ' CD ', ' CE ', ' CF '].
1 #@param {string} digits2 #@return {string[]}3 deflettercombinations (digits):4 ifdigits=="':5 return []6dic={'2':['a','b','C'],'3':['D','e','F'],'4':['g','h','I'],'5':['J','k','L'],7 '6':['m','N','o'],'7':['P','Q','R','s'],'8':['T','u','v'],'9':['W','x','y','Z']}8cmb=["']9 forDinchdigits:Tenchs=Dic[d] Oneres=[] A forPreinchCMB: - forSubinchCHS: -res+= (pre+sub) theAM =Res - returnCmb
2) The above process is implemented in Python and can take advantage of the reduce function:
1 #@param {string} digits2 #@return {string[]}3 deflettercombinations (self, digits):4 ifdigits=="':5 return []6dic={'2':'ABC','3':'def','4':'Ghi','5':'JKL','6':'MnO',7 '7':'PQRS','8':'TUV','9':'WXYZ'}8 returnReduceLambdaX, y: [pre+sub forPreinchX forSubinchDic[y]], digits,["'])
In line 8 The form of the reduce function is: Reduce (func, iterable, init).
Init is the first assignment to X, and if you do not specify INIT, the X-Initial value defaults to the iterable element. This is specified here, so the first x==["].
Iterable is a digits, which is a string containing a number symbol. Because the initial x is specified, the initial value of Y is digits[0] ... In the iterative process y will become digits[1], digits[2] ...
Func is a lambda expression:lambda x, y: [pre+sub for pre in X for sub in Dic[y]]. It contains a list-derivation. Equivalent to a function:
1 def func (X, Y): 2 Res=[] 3 For in X:4 for in dic[y]:5 res.append (pre+sub)6 return Res
In the reduce function, the result of the last calculation is a new x. Until iterable (here is digits) the iteration ends.
3) Backtracking method. The solution space of the problem is organized in the form of a tree, and a depth-first traversal (DFS) is done on this virtual tree.
1 #@param {string} digits2 #@return {string[]}3 deflettercombinations (digits):4 ifdigits=="':5 return []6dic={'2':'ABC','3':'def','4':'Ghi','5':'JKL','6':'MnO',7 '7':'PQRS','8':'TUV','9':'WXYZ'}8 9res=[]Ten BackTrack (DIC, [], 0, digits, res) One return["'. Join (LOCALSTR) forLocalstrinchRes] A - - defBackTrack (self, kvmap, LOCALSTR, index, digits, res): the ifindex==len (digits): - res.append (localstr[:]) - Else: - forCinchKvmap[digits[index]]: + Localstr.append (c) -BackTrack (Kvmap, Localstr, index+1, digits, res) +Localstr.pop ()
The above is recursion-based depth-first traversal. The exit condition of the backtracking is that the length of the string equals the length of the number string.
Note that the LINE16 is not written
Res.append (LOCALSTR)
Because this append in is a label called LOCALSTR, rather than the character content in Localstr.
Leetcode#17 letter combinations of a Phone number