from typing import List
class Solution():
def longest(self, strs: List[str]) -> str:
def lcp(str1, str2):
index = 0
min_len = min(len(str1), len(str2))
while index < min_len and str1[index] == str2[index]:
index += 1
return str1[:index]
if not strs:
return " "
prefix = strs[0]
for i in range(1, len(strs)):
prefix = lcp(prefix, strs[i])
if not prefix:
break
return prefix
test = Solution()
print(test.longest(["dcexs", "dcfdsc", "dcefs"]))
面试必会-力扣-最长公共前缀
最新推荐文章于 2024-07-12 18:12:33 发布