LeetCode 17. Letter Combinations of a Phone Number
Description
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
Note
Although the above answer is in lexicographical order, your answer could be in any order you want.
Code
- java
class Solution {
String[] phone = new String[10];
List<String> result = new ArrayList<>();
public void dfs(String digits, String temp, int cur) {
if(cur == digits.length()) {
result.add(temp);
return ;
}
int num = digits.charAt(cur) - '0';
for(int i = 0; i < phone[num].length(); i++) {
dfs(digits, temp+phone[num].charAt(i), cur+1);
}
}
public List<String> letterCombinations(String digits) {
result = new ArrayList<>();
phone[2] = "abc";
phone[3] = "def";
phone[4] = "ghi";
phone[5] = "jkl";
phone[6] = "mno";
phone[7] = "pqrs";
phone[8] = "tuv";
phone[9] = "wxyz";
String temp = "";
if(digits.length() != 0) {
dfs(digits, temp, 0);
}
return result;
}
}
- python
class Solution:
def __init__(self):
self.items = {2:"abc", 3:"def", 4:"ghi", 5:"jkl", 6:"mno", 7:"pqrs", 8:"tuv", 9:"wxyz"}
self.com = []
def dfs(self, temp, cur):
if cur == len(self.digits):
self.com.append(temp)
return
alpha = self.items[int(self.digits[cur])]
lens = len(alpha)
for i in range(0, lens):
self.dfs(temp+alpha[i], cur+1)
def letterCombinations(self, digits: str) -> List[str]:
temp = ""
idx= 0
if len(digits) != 0:
self.digits = digits
self.dfs(temp, idx)
return self.com
- Official Solution
- java
class Solution {
Map<String, String> phone = new HashMap<String, String>() {{
put("2", "abc");
put("3", "def");
put("4", "ghi");
put("5", "jkl");
put("6", "mno");
put("7", "pqrs");
put("8", "tuv");
put("9", "wxyz");
}};
List<String> output = new ArrayList<String>();
public void backtrack(String combination, String next_digits) {
// if there is no more digits to check
if (next_digits.length() == 0) {
// the combination is done
output.add(combination);
}
// if there are still digits to check
else {
// iterate over all letters which map
// the next available digit
String digit = next_digits.substring(0, 1);
String letters = phone.get(digit);
for (int i = 0; i < letters.length(); i++) {
String letter = phone.get(digit).substring(i, i + 1);
// append the current letter to the combination
// and proceed to the next digits
backtrack(combination + letter, next_digits.substring(1));
}
}
}
public List<String> letterCombinations(String digits) {
if (digits.length() != 0)
backtrack("", digits);
return output;
}
}
- python
class Solution:
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
phone = {'2': ['a', 'b', 'c'],
'3': ['d', 'e', 'f'],
'4': ['g', 'h', 'i'],
'5': ['j', 'k', 'l'],
'6': ['m', 'n', 'o'],
'7': ['p', 'q', 'r', 's'],
'8': ['t', 'u', 'v'],
'9': ['w', 'x', 'y', 'z']}
def backtrack(combination, next_digits):
# if there is no more digits to check
if len(next_digits) == 0:
# the combination is done
output.append(combination)
# if there are still digits to check
else:
# iterate over all letters which map
# the next available digit
for letter in phone[next_digits[0]]:
# append the current letter to the combination
# and proceed to the next digits
backtrack(combination + letter, next_digits[1:])
output = []
if digits:
backtrack("", digits)
return output
- Others’ Solution
class Solution:
# @param {string} digits
# @return {string[]}
def letterCombinations(self, digits):
mapping = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl',
'6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}
if len(digits) == 0:
return []
if len(digits) == 1:
return list(mapping[digits[0]])
prev = self.letterCombinations(digits[:-1])
additional = mapping[digits[-1]]
return [s + c for s in prev for c in additional]
Conclusion
- dfs
- 官方的Map接口学习了
- 第一次用py解题