forked from algorithm009-class01/algorithm009-class01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLetterCombinations.java
More file actions
27 lines (24 loc) · 922 Bytes
/
LetterCombinations.java
File metadata and controls
27 lines (24 loc) · 922 Bytes
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
package recur;
import java.util.LinkedList;
import java.util.List;
public class LetterCombinations {
public List<String> letterCombinations(String digits){
LinkedList<String> ans = new LinkedList<>();
if(digits.length() == 0) return ans;
String[] mapping = new String[]{"0","1","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
ans.add("");
while (ans.peek().length()!=digits.length()){
String remove = ans.remove();
String map = mapping[digits.charAt(remove.length())-'0'];
for (char c : map.toCharArray()){
ans.addLast(remove+c);
}
}
return ans;
}
public static void main(String[] args) {
LetterCombinations letterCombinations = new LetterCombinations();
List<String> r = letterCombinations.letterCombinations("23");
System.out.println(r.toString());
}
}