A rotation on a string is defined as removing first element and concatenating it at the end.
Given N and an array of N strings.
Your job is to predict the minimum no. of rotations on the strings so as to make all the strings equal.
If this is not possible return -1
Example
Input
4
11234
34112
41123
11234
Output
3
the no.of rotations will be n-1 for every case
can you give answer in python plss
def rotation(a,b):
if a==b:
return 0
temp=b+b
for i in range(len(b)):
if temp[i:i+len(b)]==a:
return i
else:
return -1
def min_string(a):
dic={}
maps=a[0]+a[0]
for i in range(len(a[0])):
dic[maps[i:i+len(a[0])]]=0
for ele in a:
for key in dic.keys():
dic[key]+=rotation(key,ele)
print(min(dic.values()))
min_string([“xzzwo”, “zwoxz”, “zzwox”, “xzzwo”] )
l=[“123”, “231”, “231”, “123”]
hash={}
hash1={}
temp=[]
temp.append((0,l[0]))
hash1[l[0]]=0
for j in range(1,len(l[0])):
rot=l[0][j:]+l[0][0:j]
hash1[rot]=j
temp.append((j,rot))
hash[l[0]]=temp
def solve(hash,hash1,l):
try:
for i in range(1,len(l)):
temp=[]
temp.append((l[i],0))
hash1[l[i]]+=0
for j in range(1,len(l[i])):
rot=l[i][j:]+l[i][0:j]
hash1[rot]+=j
temp.append((j,rot))
hash[l[i]]=temp
return min(hash1.values())
except:
return -1
print(solve(hash,hash1,l))
#ignore hash dictionary just used it to debug nd track
Solution
https://pastebin.com/Z8eXnPvy