+ 5
Help me to fix the problem
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 My code is: a = ["flower","flight"] if len(a) == 0: print(" ") result = " " lens = [len(str) for str in a] mim = min(lens) for i in range(1,mim+1): pre = a[0][:i] for q in a: if q[:i] == pre: result = result+pre print(result)
4 ответов
+ 4
Some little logical bugs....still not valid for more than 2 strings
a = ["flower","flight"]
if len(a) == 0:
print(" ")
result = " "
lens = [len(str) for str in a]
mim = min(lens)
for i in range(1,mim+1):
pre = a[0][:i]
for q in a[1:]:
if q[:i] == pre:
result = pre
print(result)
+ 1
Why did you use the a[1:] why not the all the elements in the list??
+ 1
pre=a[0][:i]
First element is used