Menu Close

The minimum number of strikes he will have to make.(so that all his enemies have the same name)

Rishabh has a list A of names of his N enemies. All the names are represented as string of lowercase English alphabets.
In on strike, he can choose the name of any of his enemies, remove the last character of the name, and add it to front.
For example: If the name of his enemy is “ojas”, in one strike, he can change the name to “soja”.
It will be easier for him to destroy all his enemires if all of them had the same name. What is the minimum number of strikes he will have to make so that all his enemies have the same name?
If this can’t happen, return -1

Input: First and only arguemnet is a array of strings A of size N denoting names.

Output: Return an integer corresponding to minimum number of strikes as described above.

def strike(x):
    temp=x[len(x)-1]
    x.pop(len(x)-1)
    x.insert(0,temp)
    return x
N=int(input('Enter the N: '))
string=list(map(str, input('Enter the list A: ').split(' ')))

list1=[]

temp=set(string) # converting to set, pops duplicate elements and gives out only unique values.
string=list(temp)   # Again converting it to list for manipulation, sets are no that flexible as list.
N=len(string)

for i in range(N):
    x=strike(list(string[i]))
    F=1
    for j in range(N):
        if i!=j:
            temp=list(string[j])
            p=0
            while ''.join(x) != ''.join(temp):
                temp=strike(temp)
                F+=1
                p+=1
                if p==len(temp):
                    F=0
                    break
    list1.append(F)
if min(list1)==0:
    print(-1)
else:
    print(min(list1))

Input_1:
Enter the N: 3
Enter the list A: rishabh habhris ishabhr
Output:
4


Input_2:
Enter the N: 3
Enter the list A: fcukthecode efcukthecod thecode
Output:
-1
(It is not possible to make all the strings same, so the answer is -1)


Input_3:
Enter the N: 3
Enter the list A: fcukthecode cukthecodef fcukthecode
Output:
1


Morae Q!