Rotate String
Given two strings s and goal, return true if s can become goal after some number of left rotations, and false otherwise. A left rotation moves the first…
Given two strings `s` and `goal`, return `true` if `s` can become `goal` after some number of **left rotations**, and `false` otherwise. A **left rotation** moves the first character of `s` to the end. For example, one left rotation of `"abcde"` gives `"bcdea"`. **Key insight:** every left rotation of `s` by `k` positions equals the length-`n` window of the doubled string `s + s` starting at index `k`. So `s` can rotate into `goal` if and only if `len(s) == len(goal)` and `goal` appears as a length-`n` substring of `s + s` at some start index in `[0, n-1]`. Indexing is **0-based**. **Example:** `s = "abcde"`, `goal = "cdeab"`. Doubled string = `"abcdeabcde"`. The window starting at index 2 is `"cdeab"` which equals `goal`, so the answer is `true`.
- `0 <= s.length, goal.length <= 100` - `s` and `goal` consist of lowercase English letters. - A left rotation moves `s[0]` to the end of the string. - Strings of different lengths can never be rotations of each other.
Example 1: input s = "abcde", goal = "cdeab"; output true. Rotating "abcde" left 2 times yields "cdeab". In "abcdeabcde", "cdeab" starts at index 2.
Example 2: input s = "abcde", goal = "abced"; output false. "abced" does not appear in "abcdeabcde" — no left rotation of "abcde" equals "abced".
Example 3: input s = "abcd", goal = "abcd"; output true. Zero rotations — the identity is always valid. "abcd" appears at index 0 in "abcdabcd".
Category: Strings. Topic: strings. Difficulty: Easy. Patterns: string-processing.