This commit is contained in:
parent
4ec72db468
commit
47099028a2
15
include/s0017_letter_combinations_of_a_phone_number.hpp
Normal file
15
include/s0017_letter_combinations_of_a_phone_number.hpp
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef S0017_LETTER_COMBINATIONS_OF_A_PHONE_NUMBER
|
||||||
|
#define S0017_LETTER_COMBINATIONS_OF_A_PHONE_NUMBER
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<string> letterCombinations(string digits);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
32
src/s0017_letter_combinations_of_a_phone_number.cpp
Normal file
32
src/s0017_letter_combinations_of_a_phone_number.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#include "s0017_letter_combinations_of_a_phone_number.hpp"
|
||||||
|
|
||||||
|
vector<string> Solution::letterCombinations(string digits) {
|
||||||
|
unordered_map<string, vector<string>> map;
|
||||||
|
map["2"] = vector<string>{"a", "b", "c"};
|
||||||
|
map["3"] = vector<string>{"d", "e", "f"};
|
||||||
|
map["4"] = vector<string>{"g", "h", "i"};
|
||||||
|
map["5"] = vector<string>{"j", "k", "l"};
|
||||||
|
map["6"] = vector<string>{"m", "n", "o"};
|
||||||
|
map["7"] = vector<string>{"p", "q", "r", "s"};
|
||||||
|
map["8"] = vector<string>{"t", "u", "v"};
|
||||||
|
map["9"] = vector<string>{"w", "x", "y", "z"};
|
||||||
|
if (digits.length() == 0) {
|
||||||
|
return vector<string>{};
|
||||||
|
} else {
|
||||||
|
string last = digits.substr(digits.length() - 1, 1);
|
||||||
|
digits.pop_back();
|
||||||
|
vector<string> combinations = letterCombinations(digits);
|
||||||
|
if (combinations.size() == 0) {
|
||||||
|
combinations = {""};
|
||||||
|
}
|
||||||
|
vector<string> result = {};
|
||||||
|
for (int i{0}; i < map.at(last).size(); i++) {
|
||||||
|
vector<string> temp = combinations;
|
||||||
|
for (int j{0}; j < temp.size(); j++) {
|
||||||
|
temp[j] += map.at(last)[i];
|
||||||
|
}
|
||||||
|
result.insert(result.end(), temp.begin(), temp.end());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
24
tests/s0017_letter_combinations_of_a_phone_number.cpp
Normal file
24
tests/s0017_letter_combinations_of_a_phone_number.cpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include "s0017_letter_combinations_of_a_phone_number.hpp"
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
TEST(Problem17, Case1) {
|
||||||
|
string i{"23"};
|
||||||
|
vector<string> o{"ad", "bd", "cd", "ae", "be", "ce", "af", "bf", "cf"};
|
||||||
|
Solution solution;
|
||||||
|
EXPECT_EQ(solution.letterCombinations(i), o);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Problem17, Case2) {
|
||||||
|
string i{""};
|
||||||
|
vector<string> o{};
|
||||||
|
Solution solution;
|
||||||
|
EXPECT_EQ(solution.letterCombinations(i), o);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Problem17, Case3) {
|
||||||
|
string i{"2"};
|
||||||
|
vector<string> o{"a", "b", "c"};
|
||||||
|
Solution solution;
|
||||||
|
EXPECT_EQ(solution.letterCombinations(i), o);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user