-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion17.java
More file actions
57 lines (47 loc) · 1.28 KB
/
Copy pathQuestion17.java
File metadata and controls
57 lines (47 loc) · 1.28 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
Author: Ananthanarayanan R
Section: Algorithms
Question: 17
*/
import java.util.*;
public class Question17
{
public static void store(List<String> result, String digits, int index, String[] digitCharacters, StringBuilder sb)
{
if(index>digits.length()-1)
result.add(sb.toString());
else
{
int pos = Integer.parseInt(digits.charAt(index)+"");
for(int i = 0;i<digitCharacters[pos].length();i++)
{
sb.append(digitCharacters[pos].charAt(i));
index++;
store(result,digits,index,digitCharacters,sb);
index--;
sb.deleteCharAt(sb.length()-1);
}
}
}
public static List<String> letterCombinations(String digits)
{
//Declaring the result List
List<String> result = new ArrayList<>();
//Single case
if(digits.equals(""))
return result;
//Storing the phone chatracters
String[] digitCharacters = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
//Store character in result list
int index = 0;
StringBuilder sb = new StringBuilder();
store(result,digits,index,digitCharacters,sb);
return result;
}
public static void main(String[] args)
{
String digits = "23";
List<String> result = letterCombinations(digits);
System.out.println(result);
}
}