17.Letter-Combinations-of-a-Phone-Number
17. Letter Combinations of a Phone Number
题目地址
https://leetcode.com/problems/letter-combinations-of-a-phone-number/
https://www.jiuzhang.com/solutions/palindrome-partitioning-ii/
题目描述
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"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.代码
Approach 1: Backtracking
Complexity Analysis
Time complexity : O(3^N×4^M) where
Nis the number of digits in the input that maps to 3 letters (e.g.2, 3, 4, 5, 6, 8) andMis the number of digits in the input that maps to 4 letters (e.g.7, 9), andN+Mis the total number digits in the input.Space complexity : O(3^N×4^M) since one has to keep 3^N×4^M solutions.
Last updated
Was this helpful?