-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeetCode_17_Letter_Combinations_of_a_Phone_Number.cpp
59 lines (52 loc) · 1.62 KB
/
LeetCode_17_Letter_Combinations_of_a_Phone_Number.cpp
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
58
59
/*
Given a string containing digits from 2-9 inclusive, return all possible letter
combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
Note that 1 does not map to any letters.
Example:
Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any
order you want.
*/
class Solution {
unordered_map<char, string> init() {
unordered_map< char, string > digitToChar;
digitToChar['2'] = "abc";
digitToChar['3'] = "def";
digitToChar['4'] = "ghi";
digitToChar['5'] = "jkl";
digitToChar['6'] = "mno";
digitToChar['7'] = "pqrs";
digitToChar['8'] = "tuv";
digitToChar['9'] = "wxyz";
return digitToChar;
}
void dfs( unordered_map<char, string> digitToChar,
queue<string> &result, string digits ) {
if( digits == "" ) return;
int size = result.size();
for( int i=0; i < size; i++ ) {
string s = result.front(); result.pop();
for( auto c : digitToChar[ digits[0] ] ) {
result.push( s + c );
}
}
dfs( digitToChar, result, digits.substr( 1 ) );
}
public:
vector<string> letterCombinations( string digits ) {
vector< string > results;
if( digits == "" ) return results;
unordered_map< char, string > digitToChar = init();
queue< string > result;
result.push( "" );
dfs( digitToChar, result, digits );
while( !result.empty() ) {
results.push_back( result.front() );
result.pop();
}
return results;
}
};