Write a function to find the longest common prefix string amongst an array of strings.
to compare all the strings assume the longest_prefix's length is m and there are n strings. we will need at least o(mn) time.
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if not strs:
return ''
longest_string = min(strs)
for index in range(len(longest_string)):
for string in strs:
if longest_string[index] != string[index]:
return longest_string[:index] if index > 0 else ''
return longest_string

本文介绍了一个函数,用于在一组字符串中找到最长公共前缀。通过比较所有字符串的首字母并逐步缩小范围,最终返回最长公共前缀。
2032

被折叠的 条评论
为什么被折叠?



