33 lines
1.1 KiB
C++
33 lines
1.1 KiB
C++
|
#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;
|
||
|
}
|
||
|
}
|