-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0017_letterCombinations.cc
68 lines (62 loc) · 1.78 KB
/
Problem_0017_letterCombinations.cc
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
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
void dfs(string &digits, int cur, string str, vector<string> &ans, unordered_map<char, vector<char>> &map)
{
if (cur == digits.length())
{
ans.push_back(str);
}
for (auto &c : map[digits[cur]])
{
str.push_back(c);
dfs(digits, cur + 1, str, ans, map);
str.pop_back();
}
}
vector<string> letterCombinations(string digits)
{
unordered_map<char, vector<char>> map = {{'1', {}},
{'2', {'a', 'b', 'c'}},
{'3', {'d', 'e', 'f'}},
{'4', {'g', 'h', 'i'}},
{'5', {'j', 'k', 'l'}},
{'6', {'m', 'n', 'o'}},
{'7', {'p', 'q', 'r', 's'}},
{'8', {'t', 'u', 'v'}},
{'9', {'w', 'x', 'y', 'z'}}};
vector<string> ans;
if(digits.length() == 0)
{
return ans;
}
dfs(digits, 0, "", ans, map);
return ans;
}
};
bool isVectorEuqal(vector<string> a, vector<string> b)
{
return a == b;
}
void testLetterCombinations()
{
Solution s;
vector<string> s1 = {"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"};
vector<string> s2 = {};
vector<string> s3 = {"a", "b", "c"};
EXPECT_TRUE(isVectorEuqal(s1, s.letterCombinations("23")));
EXPECT_TRUE(isVectorEuqal(s2, s.letterCombinations("")));
EXPECT_TRUE(isVectorEuqal(s3, s.letterCombinations("2")));
EXPECT_SUMMARY;
}
int main()
{
testLetterCombinations();
return 0;
}