fix: further simplify

This commit is contained in:
Lam Haoyin 2022-04-07 02:05:19 +08:00
parent 56bace58b7
commit 7e99751414
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
1 changed files with 3 additions and 12 deletions

View File

@ -3,18 +3,9 @@
class Solution { class Solution {
public: public:
static bool rotateString(const std::string& s, const std::string& goal) { static bool rotateString(const std::string& s, const std::string& goal) {
char ch = s[0]; const std::size_t n = s.length();
std::size_t n = s.length(); for (std::size_t np = goal.find(s[0], 0); np != std::string::npos; np = goal.find(s[0], np + 1))
if (goal.substr(0, np) == s.substr(n - np) && goal.substr(np) == s.substr(0, n - np))
auto verify = [&](std::size_t p) {
for (std::size_t i = 0; i < n; ++i, p = (1 + p) % n)
if (s[i] != goal[p])
return false;
return true;
};
for (std::size_t nextPos = goal.find(ch, 0); nextPos != std::string::npos; nextPos = goal.find(ch, nextPos + 1))
if (verify(nextPos))
return true; return true;
return false; return false;
} }