From 5c715daadeb6803b8d125c445b07a0402185f17b Mon Sep 17 00:00:00 2001 From: VildanWW Date: Fri, 27 Nov 2026 17:37:12 +0500 Subject: [PATCH] docs: conform brute_force_string_searching.cpp to guidelines (#2456) (#3288) * docs: conform brute_force_string_searching.cpp to guidelines (#2456) Add doxygen documentation and self-tests * Update brute_force_string_searching.cpp * Added @author * Apply suggestion from @realstealthninja Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> * Apply suggestion from @realstealthninja Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> * Apply suggestion from @realstealthninja Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> * Update brute_force_string_searching.cpp Swap int on int64_t * clang-format and clang-tidy fixes for 8262cb8e --------- Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- strings/brute_force_string_searching.cpp | 83 ++++++++++++++---------- 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/strings/brute_force_string_searching.cpp b/strings/brute_force_string_searching.cpp index f908d83bc..f5a37394d 100644 --- a/strings/brute_force_string_searching.cpp +++ b/strings/brute_force_string_searching.cpp @@ -1,32 +1,41 @@ /** * @file * @brief String pattern search - brute force + * @details + * Finds the first matching substring in the text, otherwise returns -1. + * Time complexity: O(n * m), Space complexity: O(m), where n is the length of + * the text and m is the length of the pattern. + * @author [1pierres1u](https://github.com/1pierres1u) */ -#include -#ifdef _MSC_VER -#include // use this for MS Visual C++ -#else -#include -#endif -#include -namespace string_search { +#include /// For assert +#include /// For uint64_t +#include /// For IO operations +#include /// For std::string + /** - * Find a pattern in a string by comparing the pattern to every substring. + * @namespace string_search + * @brief String search algorithms + */ +namespace string_search { + +/** + * @brief Find a pattern in a string by comparing the pattern to every + * substring. * @param text Any string that might contain the pattern. * @param pattern String that we are searching for. - * @return Index where the pattern starts in the text - * @return -1 if the pattern was not found. + * @returns Index where the pattern starts in the text + * @returns -1 if the pattern was not found. */ -int brute_force(const std::string &text, const std::string &pattern) { - size_t pat_l = pattern.length(); - size_t txt_l = text.length(); - int index = -1; - if (pat_l <= txt_l) { - for (size_t i = 0; i < txt_l - pat_l + 1; i++) { - std::string s = text.substr(i, pat_l); - if (s == pattern) { - index = i; +int64_t brute_force(const std::string& text, const std::string& pattern) { + size_t pattern_length = pattern.length(); + size_t text_length = text.length(); + int64_t index = -1; + if (pattern_length <= text_length) { + for (size_t i = 0; i < text_length - pattern_length + 1; i++) { + std::string current_substring = text.substr(i, pattern_length); + if (current_substring == pattern) { + index = static_cast(i); break; } } @@ -35,24 +44,26 @@ int brute_force(const std::string &text, const std::string &pattern) { } } // namespace string_search -using string_search::brute_force; +/** + * @brief Self-test implementations + * @returns void + */ -/** set of test cases */ -const std::vector> test_set = { - // {text, pattern, expected output} - {"a", "aa", "-1"}, {"a", "a", "0"}, {"ba", "b", "0"}, - {"bba", "bb", "0"}, {"bbca", "c", "2"}, {"ab", "b", "1"}}; +static void test() { + assert(string_search::brute_force("a", "aa") == -1); + assert(string_search::brute_force("aa", "a") == 0); + assert(string_search::brute_force("bbb", "a") == -1); + assert(string_search::brute_force("bbbbab", "ab") == 4); + + std::cout << "All tests have successfully passed!\n"; +} + +/** + * @brief Main function + * @returns 0 on exit + */ -/** Main function */ int main() { - for (const auto &i : test_set) { - int output = brute_force(i[0], i[1]); - - if (std::to_string(output) == i[2]) { - std::cout << "success\n"; - } else { - std::cout << "failure\n"; - } - } + test(); // run self test implementation. return 0; }