Letter Combinations of a Phone Number (Medium)

update Feb 19, 2018 16:51

LeetCodearrow-up-right

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.

Note:

Although the above answer is in lexicographical order, your answer could be in any order you want.

Basic Idea:

  • 思路 1: DFS

    使用dfs求解,整个递归分为 len(digits) 层,每层考虑一个 input 的数字,每层有当前层数次对应字母个数个分支,时间复杂度大约为 O(每个数字对应字母个数 ^ len(digits)),空间复杂度如果不考虑解集占用空间,为递归栈所占,O(len(digits));

    • Java Code:

  • 思路 2:BFS,iterative

    利用一个 queue,每次讲之前的生成的path扩展一层,例如input为 23, 对应字母分别为 abc, def。则我们第一层生成 a,b,c ,第二层分别针对候选的 d,e,f 进行扩展,生成最后结果:ad,ae,af,bd,be,bf,cd,ce,cf

    • Java Code: