From 7e997514144977592e7152ee44dea00b9d0e420b Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Thu, 7 Apr 2022 02:05:19 +0800 Subject: [PATCH] fix: further simplify --- cpp/2204/220407-CN.cpp | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/cpp/2204/220407-CN.cpp b/cpp/2204/220407-CN.cpp index 66bd9e9..5ac30e1 100644 --- a/cpp/2204/220407-CN.cpp +++ b/cpp/2204/220407-CN.cpp @@ -3,18 +3,9 @@ class Solution { public: static bool rotateString(const std::string& s, const std::string& goal) { - char ch = s[0]; - std::size_t n = s.length(); - - 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)) + const 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)) return true; return false; }