Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
35 lines (33 loc) · 1.35 KB

File metadata and controls

35 lines (33 loc) · 1.35 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
Author: King, wangjingui@outlook.com
Date: Dec 20, 2014
Problem: Letter Combinations of a Phone Number
Difficulty: Medium
Source: https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/
Notes:
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.
Solution: ...
*/
public class Solution {
public List<String> letterCombinations(String digits) {
ArrayList<String> res = new ArrayList<String>();
String[] keyboard = new String[]{" ","","abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
letterCombinationsRe(keyboard,res,digits,"");
return res;
}
public void letterCombinationsRe(String[] keyboard, ArrayList<String> res, String digits, String s) {
if (s.length() == digits.length()) {
res.add(s);
return;
}
String letters = keyboard[digits.charAt(s.length()) - '0'];
for (int i = 0; i < letters.length(); ++i) {
letterCombinationsRe(keyboard, res, digits, s+letters.charAt(i));
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.