-
Notifications
You must be signed in to change notification settings - Fork 87
/
LetterCombinationsOfAPhoneNumber.java
39 lines (31 loc) · 1.21 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
// problem statement -> https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
class Solution {
public List<String> letterCombinations(String digits) {
if(digits.length() == 0){
ArrayList<String> bres = new ArrayList<String>();
return bres;
}
if(digits.length() == 1){
ArrayList<String> new_bres = new ArrayList<>();
char ch = digits.charAt(0);
String code_for_number = codes[ch - 48];
for(int i = 0; i < code_for_number.length(); i++){
new_bres.add("" + code_for_number.charAt(i));
}
return new_bres;
}
char ch = digits.charAt(0);
String ss = digits.substring(1);
List<String> pans = letterCombinations(ss);
ArrayList<String> ans = new ArrayList<String>();
String code_for_number = codes[ch - 48];
for(int i = 0; i < code_for_number.length(); i++){
char chval = code_for_number.charAt(i);
for(String val : pans){
ans.add(chval + val);
}
}
return ans;
}
static String[] codes = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
}