比较简单,首先初始化设定最长公共前缀是s = strs[0],然后遍历每一个元素,依次找出s和strs[i]的最长公共前缀,更新s,即可。

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param strs string字符串一维数组 
# @return string字符串
#
class Solution:
    def longestCommonPrefix(self , strs) -> str:
        # write code here
        if len(strs) == 0:
            return ''
        s = strs[0]
        for i in range(len(strs)):
            tmp = strs[i]
            s = self.getsame(s , tmp)
        return s

    def getsame(self , s , tmp):
        i = 0 
        while i<len(s) and i<len(tmp) and s[i] == tmp[i]:
            i+=1 
        return s[:i]

strs  = ["abca","abc","abca","abc","abcc"]
print(Solution().longestCommonPrefix(strs))