Conveyor ring rotation

Write codeEasy14 min

Your task

A bottling plant runs one closed conveyor ring. Each station on the ring is labelled with a single uppercase letter, and a technician writes the ring down by starting beside whichever station they happen to be standing at, then walking with the direction of travel until they arrive back at it. Two write ups of the same ring can therefore begin at different stations. Given reference and observed, return how many stations ahead of the reference the observed write up begins: the smallest k such that reading reference from position k, wrapping past the end, reproduces observed character for character. Return -1 when no such k exists, which includes any case where the two write ups have different lengths. Two empty write ups describe the same empty ring, so return 0.

Examples

belt_shift_offset(reference: str, observed: str) -> int
Example 1
Inputbelt_shift_offset("ABCDE", "CDEAB")
Output2
Example 2
Inputbelt_shift_offset("ABCDE", "ABCDE")
Output0
Example 3
Inputbelt_shift_offset("ABC", "BAC")
Output-1