Design Patter 11: Subsets | Problem: Letter Case Permutations

main
Manish 1 year ago
parent 55ca543bdc
commit 4ab2448243

@ -0,0 +1,26 @@
/* Problem: https://leetcode.com/problems/letter-case-permutation/
*/
#include "lib_leetcode.h"
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string> solution(const std::string input) {
std::vector<std::string> 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';
}

@ -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)

@ -6,6 +6,8 @@
#include <iostream>
#include <limits>
#include <queue>
#include <string>
#include <typeinfo>
#include <vector>
/* Below TreeNode definition is copied from the problem at
@ -58,9 +60,11 @@ std::ostream &operator<<(std::ostream &out, const std::vector<T> &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<int> &vec : vec_2d)
for (const std::vector<T> &vec : vec_2d)
out << vec << ", ";
out << " \b\b}";
return out;

Loading…
Cancel
Save