From b61d03dddddfbf2da1d68208d3f026ddbcf3f499 Mon Sep 17 00:00:00 2001 From: VildanWW Date: Wed, 25 Nov 2026 00:20:06 +0500 Subject: [PATCH 1/8] docs: conform brute_force_string_searching.cpp to guidelines (#2456) Add doxygen documentation and self-tests --- strings/brute_force_string_searching.cpp | 97 +++++++++++++----------- 1 file changed, 52 insertions(+), 45 deletions(-) diff --git a/strings/brute_force_string_searching.cpp b/strings/brute_force_string_searching.cpp index f908d83bc03..a53ae4e7217 100644 --- a/strings/brute_force_string_searching.cpp +++ b/strings/brute_force_string_searching.cpp @@ -1,58 +1,65 @@ /** * @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(1). */ -#include -#ifdef _MSC_VER -#include // use this for MS Visual C++ -#else -#include -#endif -#include -namespace string_search { +#include /// For assert +#include /// For IO operations +#include /// For std::string + /** - * 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. + * @namespace string_search + * @brief String search algorithms */ -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; - break; - } - } - } - return index; -} + +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. + * @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 pattern_length = pattern.length(); + size_t text_length = text.length(); + int 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 = i; + break; + } + } + } + return index; + } } // namespace string_search -using string_search::brute_force; +/** + * @brief Self-test implementations + * @returns void + */ + +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"; +} -/** 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"}}; +/** + * @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"; - } - } - return 0; + test(); + return 0; } From 3e2580d014a0c2563c0bc07af49ea9218e9446ad Mon Sep 17 00:00:00 2001 From: VildanWW Date: Wed, 25 Nov 2026 01:44:25 +0500 Subject: [PATCH 2/8] Update brute_force_string_searching.cpp --- strings/brute_force_string_searching.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/brute_force_string_searching.cpp b/strings/brute_force_string_searching.cpp index a53ae4e7217..bfce31826c7 100644 --- a/strings/brute_force_string_searching.cpp +++ b/strings/brute_force_string_searching.cpp @@ -3,7 +3,7 @@ * @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(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. */ #include /// For assert From 2e0a8f4610f5efc9dcc2bac19e4383ce80604502 Mon Sep 17 00:00:00 2001 From: VildanWW Date: Thu, 26 Nov 2026 13:29:41 +0500 Subject: [PATCH 3/8] Added @author --- strings/brute_force_string_searching.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/strings/brute_force_string_searching.cpp b/strings/brute_force_string_searching.cpp index bfce31826c7..e777eab6017 100644 --- a/strings/brute_force_string_searching.cpp +++ b/strings/brute_force_string_searching.cpp @@ -4,6 +4,7 @@ * @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 /// For assert From 40a1543c082e7a3c9147f3fd29a70f95a11304c1 Mon Sep 17 00:00:00 2001 From: VildanWW Date: Fri, 27 Nov 2026 15:55:22 +0500 Subject: [PATCH 4/8] Apply suggestion from @realstealthninja Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- strings/brute_force_string_searching.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/strings/brute_force_string_searching.cpp b/strings/brute_force_string_searching.cpp index e777eab6017..88af966fb6d 100644 --- a/strings/brute_force_string_searching.cpp +++ b/strings/brute_force_string_searching.cpp @@ -15,7 +15,6 @@ * @namespace string_search * @brief String search algorithms */ - namespace string_search { /** * @brief Find a pattern in a string by comparing the pattern to every substring. From 1672422bcb8ad0fd744faef1ace5f1ee35df342f Mon Sep 17 00:00:00 2001 From: VildanWW Date: Fri, 27 Nov 2026 15:56:58 +0500 Subject: [PATCH 5/8] Apply suggestion from @realstealthninja Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- strings/brute_force_string_searching.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/brute_force_string_searching.cpp b/strings/brute_force_string_searching.cpp index 88af966fb6d..baea9cb5a81 100644 --- a/strings/brute_force_string_searching.cpp +++ b/strings/brute_force_string_searching.cpp @@ -60,6 +60,6 @@ static void test() { */ int main() { - test(); + test(); // run self test implementation. return 0; } From f322761122bc41d9dc6111ce50f4f8732ab63796 Mon Sep 17 00:00:00 2001 From: VildanWW Date: Fri, 27 Nov 2026 15:57:32 +0500 Subject: [PATCH 6/8] Apply suggestion from @realstealthninja Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- strings/brute_force_string_searching.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/strings/brute_force_string_searching.cpp b/strings/brute_force_string_searching.cpp index baea9cb5a81..e608a07e429 100644 --- a/strings/brute_force_string_searching.cpp +++ b/strings/brute_force_string_searching.cpp @@ -16,6 +16,7 @@ * @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. From 8262cb8e452e8d1f489cabfe6109667699eccdc2 Mon Sep 17 00:00:00 2001 From: VildanWW Date: Fri, 27 Nov 2026 17:01:19 +0500 Subject: [PATCH 7/8] Update brute_force_string_searching.cpp Swap int on int64_t --- strings/brute_force_string_searching.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/strings/brute_force_string_searching.cpp b/strings/brute_force_string_searching.cpp index e608a07e429..4763a79848b 100644 --- a/strings/brute_force_string_searching.cpp +++ b/strings/brute_force_string_searching.cpp @@ -10,6 +10,7 @@ #include /// For assert #include /// For IO operations #include /// For std::string +#include /// For uint64_t /** * @namespace string_search @@ -24,15 +25,15 @@ namespace string_search { * @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) { + int64_t brute_force(const std::string& text, const std::string& pattern) { size_t pattern_length = pattern.length(); size_t text_length = text.length(); - int index = -1; + 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 = i; + index = static_cast(i); break; } } From 38bf4210e1ea9edbd2bc21b579b39185c0cb047c Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 27 Nov 2026 12:01:50 +0000 Subject: [PATCH 8/8] clang-format and clang-tidy fixes for 8262cb8e --- strings/brute_force_string_searching.cpp | 70 ++++++++++++------------ 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/strings/brute_force_string_searching.cpp b/strings/brute_force_string_searching.cpp index 4763a79848b..f5a37394d20 100644 --- a/strings/brute_force_string_searching.cpp +++ b/strings/brute_force_string_searching.cpp @@ -3,14 +3,15 @@ * @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. + * 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 /// For assert -#include /// For IO operations -#include /// For std::string -#include /// For uint64_t +#include /// For assert +#include /// For uint64_t +#include /// For IO operations +#include /// For std::string /** * @namespace string_search @@ -18,28 +19,29 @@ */ 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. - * @returns Index where the pattern starts in the text - * @returns -1 if the pattern was not found. - */ - 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; - } - } - } - return index; - } +/** + * @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. + * @returns Index where the pattern starts in the text + * @returns -1 if the pattern was not found. + */ +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; + } + } + } + return index; +} } // namespace string_search /** @@ -48,12 +50,12 @@ namespace string_search { */ 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); + 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"; + std::cout << "All tests have successfully passed!\n"; } /** @@ -62,6 +64,6 @@ static void test() { */ int main() { - test(); // run self test implementation. - return 0; + test(); // run self test implementation. + return 0; }