rotate-string.sh — zsh

Rotate String

easy
strings

Given two strings s and goal, return true if and only if s can become goal after some number of rotations.

A rotation moves the leftmost character of the string to the rightmost end. For example, rotating "abcde" once produces "bcdea".

An equivalent way to think about the problem is: goal is a rotation of s if the two strings have the same length and goal appears inside s + s.

Input / output

  • Input: s: string, goal: string
  • Output: boolean

Constraints

  • 1 <= s.length, goal.length <= 100
  • s and goal consist of lowercase English letters.

Follow-up

How would you check whether goal is a rotation of s without explicitly building the doubled string s + s?

Examples
Example 1
Input: s = "abcde", goal = "cdeab"
Output: true
Example 2
Input: s = "abcde", goal = "abced"
Output: false
Example 3
Input: s = "aaaa", goal = "aaaa"
Output: true
🔒 6 hidden
Running will execute all 9 cases, including 6 hidden ones.