A cylindrical combination dial has its symbols arranged in a fixed ring. Rotating the dial by one click shifts every symbol one position to the left, with the symbol that falls off the left end wrapping around to the right end. Given the dial's starting arrangement and a target arrangement, determine whether the target can be produced by rotating the dial some number of clicks (possibly zero).
s — the dial's starting arrangement.goal — the target arrangement.Print YES if goal can be obtained by rotating s some number of clicks (0 or more), and NO otherwise.
s = length of goal <= 200Example 1
Input
dialcode lcodedia
Expected
YES
Explanation
Rotating "dialcode" left by 3 clicks moves the first three symbols ("dia") to the end, giving "lcode" + "dia" = "lcodedia", which matches the target exactly. So the answer is YES.
Example 2
Input
abcde abced
Expected
NO
Explanation
The five possible rotations of "abcde" are abcde, bcdea, cdeab, deabc, and eabcd. "abced" is not among them (its last two symbols are swapped rather than rotated), so the answer is NO.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →