diff --git a/cpp/2305/LC230503CN.cpp b/cpp/2305/LC230503CN.cpp new file mode 100644 index 0000000..504a92c --- /dev/null +++ b/cpp/2305/LC230503CN.cpp @@ -0,0 +1,44 @@ +#include + +/** + * 1003. Check If Word Is Valid After Substitutions + * + * Given a string s, determine if it is valid. + * + * A string s is valid if, starting with an empty string t = "", you can transform t into s after performing the following operation any number of times: + * + * Insert string "abc" into any position in t. More formally, t becomes t[left] + "abc" + t[right], where t == t[left] + t[right]. Note that t[left] and t[right] may be empty. + * Return true if s is a valid string, otherwise, return false. + */ + + +class LC230503CN { +public: + static bool isValid(const std::string&) noexcept; +}; + +int matcher(const char* str) noexcept { + auto* ptr = str; + for (int i = 0; i < 3;) { + if (i + 'a' == *str) { + // Advance + ++str; + ++i; + } else if ('a' == *str) { + int adv = matcher(str); + if (!adv) + return 0; + str += adv; + } else return 0; + } + return str - ptr; +} + +bool LC230503CN::isValid(const std::string& s) noexcept { + for (auto* ptr = s.c_str(); *ptr;) + if (int adv = matcher(ptr); adv) + ptr += adv; + else + return false; + return true; +} diff --git a/cpp/2305/defs.h b/cpp/2305/defs.h index f6a9f69..0ddf4d3 100644 --- a/cpp/2305/defs.h +++ b/cpp/2305/defs.h @@ -24,4 +24,9 @@ public: static int arraySign(const std::vector&) noexcept; }; +class LC230503CN { +public: + static bool isValid(const std::string&) noexcept; +}; + #endif //LEETCODE_CPP_DEFS_H diff --git a/cpp/2305/main.cpp b/cpp/2305/main.cpp index 53e8086..5cdba9c 100644 --- a/cpp/2305/main.cpp +++ b/cpp/2305/main.cpp @@ -12,8 +12,6 @@ std::ostream& operator<<(std::ostream& os, const std::vector& x) noexcept { } int main() { - std::cout << LC230501::average({1, 2, 3, 4}) << std::endl; - std::cout << LC230501CN::numOfMinutes(1, 0, {-1}, {0}) << std::endl; - std::cout << LC230502CN::powerfulIntegers(2, 1, 10) << std::endl; + std::cout << LC230503CN::isValid("aabbcc"); return 0; }