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 "".

17th Aug 2021, 2:48 PM
Shahir
Shahir - avatar
2 Answers
17th Aug 2021, 4:25 PM
JaScript
JaScript - avatar
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]
17th Aug 2021, 5:40 PM
Giorgos