A circular information kiosk has n name plates mounted evenly around a rotating directory wheel and numbered 0 to n - 1 in the order they appear going clockwise around the rim, with plate n - 1 sitting directly next to plate 0 so the wheel forms a closed loop. The wheel's pointer currently rests on a given starting plate. In a single move the pointer can rotate to the plate immediately clockwise of its current position or to the plate immediately counter-clockwise of it. You are given the name printed on every plate and a target name a visitor is looking for. Report the smallest number of moves needed to bring the pointer onto some plate whose printed name equals the target name exactly, or -1 if no plate carries that name. If the pointer already sits on a matching plate, 0 moves are needed.
Line 1: an integer n, the number of plates.
Line 2: n space-separated strings, the plate names in clockwise order starting from plate 0.
Line 3: an integer start, the 0-indexed plate the pointer currently rests on.
Line 4: a string target, the name the visitor is searching for.
Print a single integer: the minimum number of moves to reach a plate named target, or -1 if no such plate exists.
1 <= n <= 20000 <= start < ntarget consist of 1 to 20 lowercase English letters and digits.target may match zero, one, or many plates.Example 1
Input
6 hr sales it hr finance legal 2 hr
Expected
1
Explanation
The pointer starts on plate 2 ("it"). Plate 0 also reads "hr" but is 2 steps away going counter-clockwise (or 4 clockwise). Plate 3 reads "hr" too, and it is only 1 step away going clockwise, which is the closest match on the wheel, so the answer is 1.
Example 2
Input
4 a b c d 0 z
Expected
-1
Explanation
None of the four plates ("a", "b", "c", "d") is named "z", so no amount of spinning can reach a matching plate and the answer is -1.
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 →