def cyclic_equiv(u, v):
    n, i, j = len(u), 0, 0
if n != len(v):
    return False
while i < n and j < n:
    k = 1
while k <= n and u[(i + k) % n] == v[(j + k) % n]:
    k += 1
if k > n:
    return True
if u[(i + k) % n] > v[(j + k) % n]:
    i += k
else:
    j += k
return False
Check if a list is a rotation of another list that works with duplicates