Guest User

Untitled

a guest
May 30th, 2020
324
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def minRot(arr):
  2.     if not arr: return -1
  3.     n = len(arr[0])
  4.     cnt = {(arr[0][i:] + arr[0][:i]) : i for i in range(n)}
  5.     for s in arr[1:]:
  6.         if len(s) != n: return -1
  7.         for i in range(n):
  8.             cur = s[i:] + s[:i]
  9.             if cur not in cnt: return -1
  10.             cnt[cur] += i
  11.     return cnt[min(cnt, key = lambda x: cnt[x])]
  12. n = int(input())
  13. arr = []
  14. for i in range(n):
  15.     arr.append(input())
  16. print(minRot(arr))
RAW Paste Data