-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLetterCombinationsOfAPhoneNumber.java
46 lines (40 loc) · 1.25 KB
/
LetterCombinationsOfAPhoneNumber.java
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
38
39
40
41
42
43
44
45
46
package com.smlnskgmail.jaman.leetcodejava.medium;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
// https://leetcode.com/problems/letter-combinations-of-a-phone-number/
public class LetterCombinationsOfAPhoneNumber {
private final String digits;
public LetterCombinationsOfAPhoneNumber(String input) {
this.digits = input;
}
public List<String> solution() {
if (digits.isEmpty()) {
return Collections.emptyList();
}
String[] buttons = new String[]{
"abc",
"def",
"ghi",
"jkl",
"mno",
"prsq",
"tuv",
"wxyz"
};
List<String> result = new ArrayList<>();
result.add("");
for (int i = 0; i < digits.length(); i++) {
char c = digits.charAt(i);
List<String> newResult = new ArrayList<>();
String curr = buttons[c - '2'];
for (String layout : result) {
for (int j = 0; j < curr.length(); j++) {
newResult.add(layout + curr.charAt(j));
}
}
result = newResult;
}
return result;
}
}