-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17.java
More file actions
23 lines (20 loc) · 689 Bytes
/
17.java
File metadata and controls
23 lines (20 loc) · 689 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public List<String> letterCombinations(String digits) {
List<String> ans=new ArrayList<>();
if(digits.isEmpty()) return ans;
String[] codes={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
pad("",digits,codes,ans);
return ans;
}
static void pad(String p,String up,String[] codes,List<String> ans){
if(up.isEmpty()){
ans.add(p);
return ;
}
int digit=up.charAt(0)-'0';//to convert to int we do -'0'
for(int i=0;i<codes[digit].length();i++){
char ch=codes[digit].charAt(i);
pad(p+ch,up.substring(1),codes,ans);
}
}
}