String Rotation Check

Medium~15 min

Given two strings s1 and s2, determine if s2 is a rotation of s1.

A string rotation shifts characters from the front of a string to the back (or vice versa). For example, "waterbottle" can be rotated to "erbottlewat" by moving the first 3 characters to the end.

Return true if s2 is a rotation of s1, and false otherwise.

Examples

Example 1
Input: s1 = "waterbottle", s2 = "erbottlewat"
Output: true
Explanation: Moving "wat" from the front of "waterbottle" to the end gives "erbottlewat".
Example 2
Input: s1 = "hello", s2 = "llohe"
Output: true
Explanation: Moving "he" from the front of "hello" to the end gives "llohe".
Example 3
Input: s1 = "hello", s2 = "world"
Output: false
Explanation: "world" cannot be formed by rotating "hello".

Constraints

  • 0 <= s1.length, s2.length <= 10^5
  • s1 and s2 consist of lowercase English letters
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run