add: 220407-CN [cpp]

This commit is contained in:
Lam Haoyin 2022-04-07 01:50:47 +08:00
parent 99ed7a5980
commit e81c48f739
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 23 additions and 1 deletions

22
cpp/2204/220407-CN.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <string>
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;
};
std::size_t pos = 0;
for (std::size_t nextPos = goal.find(ch, pos); nextPos != std::string::npos; nextPos = goal.find(ch, pos = nextPos + 1))
if (verify(nextPos))
return true;
return false;
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2204)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2204 220406-CN.cpp) ADD_EXECUTABLE(2204 220407-CN.cpp)