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
37 lines (33 loc) · 1.16 KB

File metadata and controls

37 lines (33 loc) · 1.16 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
36
37
import java.util.*;
class Problem17 {
public static void main(String[] args) {
System.out.println(new Problem17().letterCombinations("9"));
}
public List<String> letterCombinations(String digits) {
List<String> res = new ArrayList<>();
if (digits.length() ==0) return res;
char c = digits.charAt(0);
char[] arr = null;
char pos = (char)('a' + 3 * (c-'2'));
if(c == '7' ) {
arr = new char[]{(char)pos, (char)(pos+1), (char)(pos+2), (char)(pos+3)};
}else
if(c == '8') {
arr = new char[]{(char)(pos+1), (char)(pos+2), (char)(pos+3)};
} else
if(c == '9') {
arr = new char[]{(char)(pos+1), (char)(pos+2), (char)(pos+3),(char)(pos+4)};
} else arr = new char[]{(char)pos, (char)(pos+1), (char)(pos+2)};
List<String> resSub = letterCombinations(digits.substring(1));
for (int i = 0; i < arr.length; i++) {
if(resSub.size() > 0) {
for (int j = 0; j < resSub.size(); j++) {
res.add(String.valueOf(arr[i]) + resSub.get(j));
}
} else {
res.add(String.valueOf(arr[i]));
}
}
return res;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.