-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
33 lines (28 loc) · 1.02 KB
/
Main.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
package com.company;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// write your code here
}
public List<String> letterCombinations(String digits) {
if(digits.isEmpty()) return new ArrayList<>();
String[] board = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
List<String> res = new ArrayList<>();
dfs(digits, 0, new StringBuilder(), board, res);
return res;
}
public void dfs(String digits, int index, StringBuilder word, String[] board, List<String> res) {
if (index == digits.length()) {
res.add(word.toString());
return;
}
for (int i = 0; i < board[digits.charAt(index) - '0'].length(); i++) {
word.append(board[digits.charAt(index) - '0'].charAt(i));
dfs(digits, index + 1, word, board, res);
word.deleteCharAt(word.length() - 1);
}
}
}