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!
- Convert Numbers into Roman Numerals
- Return the size of the longest sub-string
- Return all strings in words which is sub-string of another word in any order.
- Calculate the step by step sum of startValue plus elements in array.
- Find the minimum number of Fibonacci numbers whose sum is equal to k.
- Given a string s of zeros and ones, return the maximum score after splitting the string.
- Write a efficient functions to find floor of x.
- Print all numbers less than n which are having digits only 3 or 7 or both.
- Function that returns true if given array can be divided into pairs.
- Find the smallest element in the list that is larger than the given target.
- Print the First N prime numbers.
- The Chef’s Binary Tree.
- The minimum number of strikes he will have to make.(so that all his enemies have the same name)
- Sum of Natural Numbers.
- Sum of the Input.
- Find the Sum of the Series: 1 + 1/2 + 1/3 + .. + 1/N.
- Split elements into even and odd list
- Python Program to Merge Two Lists and Sort it.
- Median of Three.
- Find the Largest Number in a List.