1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ class Solution {
6
+ public:
7
+ vector<string> letterCombinations (string digits) {
8
+ if (digits.size () == 0 ) return {};
9
+ unordered_map<char , vector<char >> hash_map = {
10
+ {' 2' , {' a' , ' b' , ' c' }},
11
+ {' 3' , {' d' , ' e' , ' f' }},
12
+ {' 4' , {' g' , ' h' , ' i' }},
13
+ {' 5' , {' j' , ' k' , ' l' }},
14
+ {' 6' , {' m' , ' n' , ' o' }},
15
+ {' 7' , {' p' , ' q' , ' r' , ' s' }},
16
+ {' 8' , {' t' , ' u' , ' v' }},
17
+ {' 9' , {' w' , ' x' , ' y' , ' z' }}
18
+ };
19
+
20
+ vector<string> res;
21
+ vector<char > temp;
22
+ dfs (digits, 0 , hash_map, temp, res);
23
+ return res;
24
+
25
+ }
26
+ void dfs (string &digits, int idx, unordered_map<char , vector<char >> &hash_map, vector<char > &temp,vector<string> &res)
27
+ {
28
+ int n = digits.size (), m = hash_map[digits[idx]].size ();
29
+ if (idx == n)
30
+ res.emplace_back (temp.begin (), temp.end ());
31
+
32
+ for (int i = 0 ; i < m; ++i) {
33
+ temp.push_back (hash_map[digits[idx]][i]);
34
+ dfs (digits, idx+1 , hash_map, temp, res);
35
+ temp.pop_back ();
36
+ }
37
+ }
38
+ };
0 commit comments