From 80c7cab5b686591a6ba741ae5450328b1a350777 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Wed, 10 Apr 2024 23:38:10 +0400 Subject: [PATCH] embolding cpp --- cpp/string_embolding.cpp | 73 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 cpp/string_embolding.cpp diff --git a/cpp/string_embolding.cpp b/cpp/string_embolding.cpp new file mode 100644 index 0000000..f3185c8 --- /dev/null +++ b/cpp/string_embolding.cpp @@ -0,0 +1,73 @@ +#include +#include +#include + +std::vector buildMask(const std::string& main_string, const std::vector& substrings) +{ + std::vector bold_mask(main_string.size(), false); + for (const auto& substring : substrings) + { + const auto substring_index = main_string.find(substring); + if (substring_index != main_string.npos) + { + for (size_t mask_i = substring_index; mask_i < substring_index + substring.size(); ++mask_i) + { + bold_mask[mask_i] = true; + } + } + } + + return bold_mask; +} + +std::string emboldenString(const std::string& main_string, const std::vector& substrings) +{ + std::vector bold_mask = buildMask(main_string, substrings); + + std::string embolded_string; + bool is_bold_now = false; + for (size_t mask_i = 0; mask_i < bold_mask.size(); ++mask_i) + { + if (!is_bold_now) + { + if (bold_mask[mask_i]) + { + embolded_string += ""; + embolded_string += main_string[mask_i]; + is_bold_now = true; + } + else + { + embolded_string += main_string[mask_i]; + } + } + else + { + if (bold_mask[mask_i]) + { + embolded_string += main_string[mask_i]; + + } + else + { + embolded_string += ""; + embolded_string += main_string[mask_i]; + is_bold_now = false; + } + } + } + + if (is_bold_now) + { + embolded_string += ""; + } + + return embolded_string; +} + +int main() +{ + std::string main_string = "I despise leetcode kind of problems."; + std::vector substrings = {"despise", "pise", "I", "s."}; + std::cout << emboldenString(main_string, substrings) << '\n'; +} \ No newline at end of file