add: 230503-CN
This commit is contained in:
parent
32f7bd3bd0
commit
611beb5be6
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -24,4 +24,9 @@ public:
|
||||||
static int arraySign(const std::vector<int>&) noexcept;
|
static int arraySign(const std::vector<int>&) noexcept;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class LC230503CN {
|
||||||
|
public:
|
||||||
|
static bool isValid(const std::string&) noexcept;
|
||||||
|
};
|
||||||
|
|
||||||
#endif //LEETCODE_CPP_DEFS_H
|
#endif //LEETCODE_CPP_DEFS_H
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,6 @@ std::ostream& operator<<(std::ostream& os, const std::vector<T>& x) noexcept {
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::cout << LC230501::average({1, 2, 3, 4}) << std::endl;
|
std::cout << LC230503CN::isValid("aabbcc");
|
||||||
std::cout << LC230501CN::numOfMinutes(1, 0, {-1}, {0}) << std::endl;
|
|
||||||
std::cout << LC230502CN::powerfulIntegers(2, 1, 10) << std::endl;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue