-
Notifications
You must be signed in to change notification settings - Fork 5
/
LetterCombinations.java
36 lines (32 loc) · 1.01 KB
/
LetterCombinations.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
/*https://leetcode.com/problems/letter-combinations-of-a-phone-number/*/
class Solution {
String[] chs;
List<String> l;
Solution()
{
//standard letter combinations
chs = new String[]{"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
l = new ArrayList<String>();
}
public List<String> letterCombinations(String digits) {
//edge case
if (digits.length() == 0) return new ArrayList<String>();
Solution s = new Solution();
//recursion call
s.generateCombinations(0,"",digits);
return s.l;
}
public void generateCombinations(int index, String curr, String digits)
{
//base case
if (index == digits.length())
{
l.add(curr);
return;
}
int ind = Integer.parseInt(Character.toString(digits.charAt(index)))-2;
/*recursion*/
for (int i = 0; i < chs[ind].length(); ++i)
generateCombinations(index+1,curr+(chs[ind].charAt(i)),digits);
}
}