Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.

Example:

Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

9字键盘的所有字母组合,实现没什么难度
不过这道题可以当做reduce函数的例子,很典型

 1 class Solution:
 2     def letterCombinations(self, digits):
 3         """
 4         :type digits: str
 5         :rtype: List[str]
 6         """
 7         if digits == '' : return []
 8         digit_map = {}
 9         digit_map['1'] = ['*']
10         digit_map['2'] = ['a','b','c']
11         digit_map['3'] = ['d','e','f']
12         digit_map['4'] = ['g','h','i']
13         digit_map['5'] = ['j','k','l']
14         digit_map['6'] = ['m','n','o']
15         digit_map['7'] = ['p','q','r','s']
16         digit_map['8'] = ['t','u','v']
17         digit_map['9'] = ['w','x','y','z']
18         def numchar2char(s) :
19             return digit_map[s]
20         def combchars(a, b) :
21             l = []
22             for s1 in a:
23                 for s2 in b:
24                     l.append(s1 + s2)
25             return l
26         return reduce(combchars, map(numchar2char, list(digits)))