1
+ //LeetCode 17. Letter Combinations of a Phone Number
2
+ //Question - https://leetcode.com/problems/letter-combinations-of-a-phone-number/
3
+
4
+ class Solution {
5
+ List <String > res ;
6
+ Map <Character , List <Character >> map ;
7
+
8
+ public List <String > letterCombinations (String digits ) {
9
+ int n = digits .length ();
10
+ res = new ArrayList <>();
11
+
12
+ if (n == 0 ) return res ;
13
+ char str [] = new char [n ];
14
+
15
+ map = new HashMap <>();
16
+ map .put ('2' , Arrays .asList ('a' , 'b' , 'c' ));
17
+ map .put ('3' , Arrays .asList ('d' , 'e' , 'f' ));
18
+ map .put ('4' , Arrays .asList ('g' , 'h' , 'i' ));
19
+ map .put ('5' , Arrays .asList ('j' , 'k' , 'l' ));
20
+ map .put ('6' , Arrays .asList ('m' , 'n' , 'o' ));
21
+ map .put ('7' , Arrays .asList ('p' , 'q' , 'r' , 's' ));
22
+ map .put ('8' , Arrays .asList ('t' , 'u' , 'v' ));
23
+ map .put ('9' , Arrays .asList ('w' , 'x' , 'y' , 'z' ));
24
+
25
+ helper (str , digits , 0 );
26
+ return res ;
27
+
28
+ }
29
+
30
+ public void helper (char str [], String digits , int index ){
31
+ if (index == digits .length ()){
32
+ res .add (String .valueOf (str ));
33
+ return ;
34
+ }
35
+
36
+ char currChar = digits .charAt (index );
37
+ List <Character > chars = map .get (currChar );
38
+
39
+ for (char c : chars ){
40
+ str [index ] = c ;
41
+ helper (str , digits , index + 1 );
42
+ }
43
+ }
44
+ }
0 commit comments