0
Help me to fix the error?
a = [] b = input().split(" ") for i in b: a.append(i) q = len(a[0]) while q>0: q = q-1 for j in a: y = len(j) while y>0: y = y-1 if a[q-1]== a[y-1]: print("Hello") else: print(" No!") Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "".
2 Answers
+ 1
This will help you:
https://code.sololearn.com/cdaDF92v652M/?ref=app
0
Shahir What you have to do is check the first character if its the same in all the strings, then check the second, then the third etc, until at the current position they are not all equal.
def longestCommonPrefix(words):
k = 0
while k<len(words) and all(words[i][k] == words[0][k] for i in range(len(words))):
k += 1
return words[0][:k]