From 4ab24482438524eddb7a67709c3f4a65710be280 Mon Sep 17 00:00:00 2001 From: Manish Date: Fri, 10 Mar 2023 14:55:05 +1100 Subject: [PATCH] Design Patter 11: Subsets | Problem: Letter Case Permutations --- 11_letter_case_permutation.cpp | 26 ++++++++++++++++++++++++++ CMakeLists.txt | 2 ++ lib_leetcode.h | 10 +++++++--- 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 11_letter_case_permutation.cpp diff --git a/11_letter_case_permutation.cpp b/11_letter_case_permutation.cpp new file mode 100644 index 0000000..f18c8ac --- /dev/null +++ b/11_letter_case_permutation.cpp @@ -0,0 +1,26 @@ +/* Problem: https://leetcode.com/problems/letter-case-permutation/ + */ + +#include "lib_leetcode.h" +#include +#include +#include + +std::vector solution(const std::string input) { + std::vector perms = {""}; + for (const char c : input) { + for (std::size_t i = 0, upto = perms.size(); i < upto; i++) { + if (std::isalpha(c)) + perms.push_back(perms[i] + (char)std::toupper(c)); + perms[i] += std::tolower(c); + } + } + return perms; +} + +int main() { + std::cout << solution("a1b2") << '\n' + << solution("3z4") << '\n' + << solution("mQe") << '\n' + << solution("03fH") << '\n'; +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c1b9b9..80f6d29 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,3 +24,5 @@ add_executable(8_binary_tree_level_order_traversal 8_binary_tree_level_order_tra add_executable(9_path_sum_iii 9_path_sum_iii.cpp) add_executable(10_find_the_median_of_a_number_stream 10_find_the_median_of_a_number_stream.cpp) + +add_executable(11_letter_case_permutation 11_letter_case_permutation.cpp) \ No newline at end of file diff --git a/lib_leetcode.h b/lib_leetcode.h index d954a40..279987f 100644 --- a/lib_leetcode.h +++ b/lib_leetcode.h @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include /* Below TreeNode definition is copied from the problem at @@ -58,9 +60,11 @@ std::ostream &operator<<(std::ostream &out, const std::vector &vec) { out << "}"; return out; } - for (int num : vec) { - std::cout << num << ", "; + for (T e : vec) { + std::cout << e << ", "; } + if (typeid(vec[0]) == typeid(std::string)) + out << '\b'; out << " \b\b}"; return out; } @@ -73,7 +77,7 @@ std::ostream &operator<<(std::ostream &out, out << "}"; return out; } - for (const std::vector &vec : vec_2d) + for (const std::vector &vec : vec_2d) out << vec << ", "; out << " \b\b}"; return out;