Files
2026-05-04 08:12:20 +08:00

448 B

C++ Tricks

Rotate String Check   string trick search

Front

How to check if string s can be rotated to match string goal in O(n)?

Back

bool rotateString(string s, string goal) {
    return s.size() == goal.size() && (s + s).find(goal) != string::npos;
}

If goal is a substring of s + s, then s can be rotated to match goal.