#+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~.