Check If One String Is a Rotation of Another
Implement isRotation
Given two strings
s and goal, return true if and only if s can become goal after some number of left rotations — repeatedly moving characters from the front of the string to the back (e.g. rotating "abcde" by 2 gives "cdeab").
Trying every rotation offset and rebuilding the string each time works, but it's wasteful. The key trick: every rotation of s is guaranteed to appear as a contiguous substringSubstringA contiguous run of characters taken from within a larger string — unlike a subsequence, the characters must be adjacent and in order. somewhere inside s + s. So the whole problem reduces to one substring-containment check on the doubled string.
Example 1:
Input: s = "abcde", goal = "cdeab"
Output: true
Example 2:
Input: s = "abcde", goal = "abced"
Output: false
Example 3:
Input: s = "a", goal = "a"
Output: true
+ 7 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ s.length, goal.length ≤ 10⁴ - ●
s and goal consist of lowercase English letters
s =
abcde
goal =
cdeab