Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
Input:Digit string "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
Subscribe to see which companies asked this question
分析:
python写比较简单。
class Solution(object):
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
mm={}
mm['0']=[" "]
mm['1']=["*"]
mm["2"]=["a","b","c"]
mm["3"]=["d","e","f"]
mm['4']=["g","h","i"]
mm["5"]=["j","k","l"]
mm["6"]=["m","n","o"]
mm['7']=["p","q","r","s"]
mm["8"]=["t","u","v"]
mm["9"]=["w","x","y","z"]
result=[]
for iter in digits:
if not result:
result=mm[iter]
else:
tempresult=result
result=[]
for temp in tempresult:
for iters in mm[iter]:
s=temp+iters
result.append(s)
return result