FunctionRacerProblemsRace

Left Steps to Match

EasyArraysPython, JavaScript, TypeScript, Java, C++

Rotating a list one step to the left moves its first item to the end: [1, 2, 3, 4] becomes [2, 3, 4, 1].

Return the smallest number of left steps that turns a into b, where 0 steps means the two lists are already identical. If no number of steps works, which is always the case when the lengths differ, return -1. Repeated values are allowed, so several step counts may work; return the smallest.

Example 1

a [1,2,3,4]
b [3,4,1,2]
returns 2

One step gives [2, 3, 4, 1], a second step gives [3, 4, 1, 2].

Example 2

a [1,2,3]
b [3,2,1]
returns -1

No rotation of [1, 2, 3] reverses it.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Arrays problems