184. Rotation Twins

A jeweller strings coloured beads on a loop and writes the colours as a lowercase string. Two strings are called rotation twins when one can be turned into the other by taking some letters from the front and moving them, in the same order, to the back. Taking zero letters is allowed, so a string is always a rotation twin of itself.

Given the strings s and goal, return true if goal can be obtained from s by such a move, and false otherwise. Strings of different lengths can never be twins.

Example 1

Input:
s = "abcde", goal = "cdeab"
Output:
true
Explanation:

Moving the front letters ab to the back turns abcde into cdeab.

Example 2

Input:
s = "abcde", goal = "abced"
Output:
false
Explanation:

No rotation of abcde reads abced, because rotations keep neighbouring letters together in the same cyclic order.

Example 3

Input:
s = "ab", goal = "ab"
Output:
true
Explanation:

Moving zero letters gives the string back, so identical strings are twins.

Constraints

1 ≤ s.length, goal.length ≤ 100

Both strings consist of lowercase English letters

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…