From dab34e04adb41ffcef04570bdc8c2577ee1447eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Borba?= Date: Sat, 19 Sep 2026 09:23:00 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9F=A1=20Port=20problem=2017=20(Letter=20?= =?UTF-8?q?Combinations=20of=20a=20Phone=20Number)=20from=20Swift=20to=20C?= =?UTF-8?q?++23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces swift/17.swift with solutions/0001-0100/0017-letter-combinations-of-a-phone-number/. --- .../README.md | 11 +++++ .../solution.cpp | 38 +++++++++++++++ .../solution.hpp | 12 +++++ .../test.cpp | 10 ++++ swift/17.swift | 48 ------------------- 5 files changed, 71 insertions(+), 48 deletions(-) create mode 100644 solutions/0001-0100/0017-letter-combinations-of-a-phone-number/README.md create mode 100644 solutions/0001-0100/0017-letter-combinations-of-a-phone-number/solution.cpp create mode 100644 solutions/0001-0100/0017-letter-combinations-of-a-phone-number/solution.hpp create mode 100644 solutions/0001-0100/0017-letter-combinations-of-a-phone-number/test.cpp delete mode 100644 swift/17.swift diff --git a/solutions/0001-0100/0017-letter-combinations-of-a-phone-number/README.md b/solutions/0001-0100/0017-letter-combinations-of-a-phone-number/README.md new file mode 100644 index 0000000..5ee21c7 --- /dev/null +++ b/solutions/0001-0100/0017-letter-combinations-of-a-phone-number/README.md @@ -0,0 +1,11 @@ +# 17. Letter Combinations of a Phone Number + +[LeetCode problem 17](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) + +- **Difficulty**: Medium +- **Tags**: hash-table, string, backtracking + +## Description + +Given a string of digits from `2` to `9`, return all possible letter +combinations the digits could represent on a phone keypad, in any order. diff --git a/solutions/0001-0100/0017-letter-combinations-of-a-phone-number/solution.cpp b/solutions/0001-0100/0017-letter-combinations-of-a-phone-number/solution.cpp new file mode 100644 index 0000000..9b906f9 --- /dev/null +++ b/solutions/0001-0100/0017-letter-combinations-of-a-phone-number/solution.cpp @@ -0,0 +1,38 @@ +#include "solution.hpp" + +#include + +namespace leetcode::p0017 { + +std::vector solve(const std::string& digits) { + if (digits.empty()) { + return {}; + } + + static const std::unordered_map keypad{ + {'2', "abc"}, + {'3', "def"}, + {'4', "ghi"}, + {'5', "jkl"}, + {'6', "mno"}, + {'7', "pqrs"}, + {'8', "tuv"}, + {'9', "wxyz"}, + }; + + std::vector result{""}; + + for (char digit : digits) { + std::vector next; + for (const auto& combination : result) { + for (char letter : keypad.at(digit)) { + next.push_back(combination + letter); + } + } + result = std::move(next); + } + + return result; +} + +} // namespace leetcode::p0017 diff --git a/solutions/0001-0100/0017-letter-combinations-of-a-phone-number/solution.hpp b/solutions/0001-0100/0017-letter-combinations-of-a-phone-number/solution.hpp new file mode 100644 index 0000000..92fe440 --- /dev/null +++ b/solutions/0001-0100/0017-letter-combinations-of-a-phone-number/solution.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +namespace leetcode::p0017 { + +// optimized: O(4^n * n) time (n = digits.size()), O(4^n * n) space — build +// the combinations iteratively, one digit at a time. +std::vector solve(const std::string& digits); + +} // namespace leetcode::p0017 diff --git a/solutions/0001-0100/0017-letter-combinations-of-a-phone-number/test.cpp b/solutions/0001-0100/0017-letter-combinations-of-a-phone-number/test.cpp new file mode 100644 index 0000000..0a99db1 --- /dev/null +++ b/solutions/0001-0100/0017-letter-combinations-of-a-phone-number/test.cpp @@ -0,0 +1,10 @@ +#include + +#include "solution.hpp" + +TEST_CASE("Problem 17", "[p0017]") { + CHECK(leetcode::p0017::solve("").empty()); + CHECK(leetcode::p0017::solve("2") == std::vector{"a", "b", "c"}); + CHECK(leetcode::p0017::solve("23") == + std::vector{"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"}); +} diff --git a/swift/17.swift b/swift/17.swift deleted file mode 100644 index 5f01622..0000000 --- a/swift/17.swift +++ /dev/null @@ -1,48 +0,0 @@ -class Solution { - let dict: [Character: [String]] = [ - "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"] - ] - - func letterCombinations(_ digits: String) -> [String] { - guard digits.count > 0 else { - return [] - } - - let letters = getLetters(digits) - return generateCombinations(letters) - } - - private func getLetters(_ digits: String) -> [[String]] { - var letters: [[String]] = [] - for digit in digits { - if let letter = dict[digit] { - letters.append(letter) - } - } - return letters - } - - private func generateCombinations(_ letters: [[String]]) -> [String] { - var result: [String] = [""] - for letterGroup in letters { - var newResult: [String] = [] - for combination in result { - for letter in letterGroup { - newResult.append(combination + letter) - } - } - result = newResult - } - return result - } -} - -let solution = Solution() -print(solution.letterCombinations("23")) // ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"] \ No newline at end of file