This commit is contained in:
2026-05-04 08:12:20 +08:00
parent 6b9a0d3161
commit 69676a84be
4 changed files with 159 additions and 1 deletions
+17
View File
@@ -0,0 +1,17 @@
#+title: C++ Tricks
* Rotate String Check :string:trick:search:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 20260504001
:END:
** Front
How to check if string ~s~ can be rotated to match string ~goal~ in O(n)?
** Back
#+begin_src c++
bool rotateString(string s, string goal) {
return s.size() == goal.size() && (s + s).find(goal) != string::npos;
}
#+end_src
If ~goal~ is a substring of ~s + s~, then ~s~ can be rotated to match ~goal~.