From 47099028a2324506aa4f00f4722f5979bcc77a52 Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Fri, 4 Nov 2022 15:50:54 +0800 Subject: [PATCH] s0017 --- ..._letter_combinations_of_a_phone_number.hpp | 15 +++++++++ ..._letter_combinations_of_a_phone_number.cpp | 32 +++++++++++++++++++ ..._letter_combinations_of_a_phone_number.cpp | 24 ++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 include/s0017_letter_combinations_of_a_phone_number.hpp create mode 100644 src/s0017_letter_combinations_of_a_phone_number.cpp create mode 100644 tests/s0017_letter_combinations_of_a_phone_number.cpp diff --git a/include/s0017_letter_combinations_of_a_phone_number.hpp b/include/s0017_letter_combinations_of_a_phone_number.hpp new file mode 100644 index 0000000..44c0c86 --- /dev/null +++ b/include/s0017_letter_combinations_of_a_phone_number.hpp @@ -0,0 +1,15 @@ +#ifndef S0017_LETTER_COMBINATIONS_OF_A_PHONE_NUMBER +#define S0017_LETTER_COMBINATIONS_OF_A_PHONE_NUMBER + +#include +#include +#include + +using namespace std; + +class Solution { + public: + vector letterCombinations(string digits); +}; + +#endif diff --git a/src/s0017_letter_combinations_of_a_phone_number.cpp b/src/s0017_letter_combinations_of_a_phone_number.cpp new file mode 100644 index 0000000..4e6be4f --- /dev/null +++ b/src/s0017_letter_combinations_of_a_phone_number.cpp @@ -0,0 +1,32 @@ +#include "s0017_letter_combinations_of_a_phone_number.hpp" + +vector Solution::letterCombinations(string digits) { + unordered_map> map; + map["2"] = vector{"a", "b", "c"}; + map["3"] = vector{"d", "e", "f"}; + map["4"] = vector{"g", "h", "i"}; + map["5"] = vector{"j", "k", "l"}; + map["6"] = vector{"m", "n", "o"}; + map["7"] = vector{"p", "q", "r", "s"}; + map["8"] = vector{"t", "u", "v"}; + map["9"] = vector{"w", "x", "y", "z"}; + if (digits.length() == 0) { + return vector{}; + } else { + string last = digits.substr(digits.length() - 1, 1); + digits.pop_back(); + vector combinations = letterCombinations(digits); + if (combinations.size() == 0) { + combinations = {""}; + } + vector result = {}; + for (int i{0}; i < map.at(last).size(); i++) { + vector 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; + } +} diff --git a/tests/s0017_letter_combinations_of_a_phone_number.cpp b/tests/s0017_letter_combinations_of_a_phone_number.cpp new file mode 100644 index 0000000..9690cca --- /dev/null +++ b/tests/s0017_letter_combinations_of_a_phone_number.cpp @@ -0,0 +1,24 @@ +#include "s0017_letter_combinations_of_a_phone_number.hpp" + +#include + +TEST(Problem17, Case1) { + string i{"23"}; + vector o{"ad", "bd", "cd", "ae", "be", "ce", "af", "bf", "cf"}; + Solution solution; + EXPECT_EQ(solution.letterCombinations(i), o); +} + +TEST(Problem17, Case2) { + string i{""}; + vector o{}; + Solution solution; + EXPECT_EQ(solution.letterCombinations(i), o); +} + +TEST(Problem17, Case3) { + string i{"2"}; + vector o{"a", "b", "c"}; + Solution solution; + EXPECT_EQ(solution.letterCombinations(i), o); +}