add: 230503-CN

This commit is contained in:
Eatswap 2023-05-03 11:44:22 +08:00
parent 32f7bd3bd0
commit 611beb5be6
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
3 changed files with 50 additions and 3 deletions

44
cpp/2305/LC230503CN.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <string>
/**
* 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;
}

View File

@ -24,4 +24,9 @@ public:
static int arraySign(const std::vector<int>&) noexcept;
};
class LC230503CN {
public:
static bool isValid(const std::string&) noexcept;
};
#endif //LEETCODE_CPP_DEFS_H

View File

@ -12,8 +12,6 @@ std::ostream& operator<<(std::ostream& os, const std::vector<T>& 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;
}