Left Steps to Match
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
1 <= len(a), len(b) <= 100-1000 <= a[i], b[i] <= 1000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Arrays problems
- Missing RangesEasy
- Monotonic ArrayEasy
- Nearest Aligned PointEasy
- Outside InEasy
- Pascal RowEasy
- Plurality WinnerEasy
- Range Sum QueriesEasy
- Remove ElementEasy